DEV Community

HarmonyOS
HarmonyOS

Posted on

Failed to create AVSession, error code 6600101

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

  1. Single ownership: Keep a single module-level reference to the current AVSession.
  2. Add guards: Track creating / destroying states to prevent overlapping calls.
  3. Destroy sequentially: Always await session.destroy() before creating a new one.
  4. Clear reference: Set session = null after a successful (or finally) destroy.
  5. Create sequentially: Only call createAVSession when no session exists and no destroy is in progress.
  6. 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);
}
Enter fullscreen mode Exit fullscreen mode

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 AVSession per 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

  • createAVSession and usage notes:

https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-avsession#avsessioncreateavsession10

  • Error codes (6600101 duplicate create):

https://developer.huawei.com/consumer/en/doc/harmonyos-references/errorcode-avsession#section6600101-%E4%BC%9A%E8%AF%9D%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%BC%82%E5%B8%B8

Written by Bunyamin Eymen Alagoz

Top comments (0)