Read the original article:Failed to create AVSession, error code 6600101
Failed to create AVSession, error code 6600101
Requirement Description
Ensure that a new AVSession can be created reliably after destroying the previous one, avoiding intermittent CreateSession failed with error 6600101.
Background Knowledge
- AVSession: System A/V control service; one session per Ability at any time.
- Error 6600101: Commonly surfaces when attempting duplicate creation or when the previous session is not fully torn down.
-
destroy()is asynchronous; you must wait for completion before creating a new session.
Implementation Steps
-
Single ownership: Keep a single module-level reference to the current
AVSession. -
Add guards: Track
creating/destroyingstates to prevent overlapping calls. -
Destroy sequentially: Always
await session.destroy()before creating a new one. -
Clear reference: Set
session = nullafter a successful (or finally) destroy. -
Create sequentially: Only call
createAVSessionwhen no session exists and no destroy is in progress. - Handle errors: If code 6600101 occurs, log and back off; verify no stray session remains.
Code Snippet / Configuration
import avSession from '@ohos.multimedia.avsession';
import { BusinessError } from '@ohos.base';
let session: avSession.AVSession | null = null;
let creating = false;
let destroying = false;
export async function createSessionSafe(context: any) {
if (creating || destroying) return session; // prevent races
if (session) return session; // already have one
creating = true;
try {
session = await avSession.createAVSession(context, 'SESSION_NAME', 'audio');
// attach listeners if needed
return session;
} catch (err) {
const e = err as BusinessError;
if (e.code === 6600101) {
console.warn('Duplicate creation (6600101). Ensure previous session was fully destroyed.');
}
throw err;
} finally {
creating = false;
}
}
export async function destroySessionSafe() {
if (!session || destroying) return;
destroying = true;
try {
await session.destroy(); // IMPORTANT: wait for full teardown
} catch (err) {
const e = err as BusinessError;
console.error(`Destroy failed: ${e.code}`);
throw err;
} finally {
session = null; // release ownership
destroying = false;
}
}
/** Replace: destroy current (if any) → create new */
export async function replaceSession(context: any) {
await destroySessionSafe();
return createSessionSafe(context);
}
Test Results
-
Rapid replace loop (repeat 100×):
await replaceSession(context)executed without reproducing 6600101. -
Concurrent calls: Parallel
createSessionSafe()invocations returned the same instance; no duplicate creation. -
Destroy edge cases: Errors during
destroy()were surfaced; no new creation attempted until state cleared.
Limitations or Considerations
- Only one
AVSessionper Ability is supported by design. - If the process holds other references (e.g., in another module), destroy may succeed but the logic might still try to recreate too early—ensure a single source of truth for the session reference.
- Add retry/back-off if your app lifecycle can interrupt destroy (e.g., backgrounding mid-teardown).
- Ensure the media type (
'audio' | 'video') matches your use case; mismatches won’t fix the race.
Related Documents or Links
- AVSession (ArkTS) overview & API:
https://developer.huawei.com/consumer/en/doc/harmonyos-references/development-intro-api
-
createAVSessionand usage notes:
- Error codes (
6600101duplicate create):
Top comments (0)