import { Room, RoomEvent, Track, } from 'livekit-c...
# ask-ai
c
import { Room, RoomEvent, Track, } from 'livekit-client' in client side after disconnect the in server console i got this error. deisconenct and delete the room is good options ?? /root/.pm2/logs/calling-agent-out.log last 15 lines: 14|calling | error: null 14|calling | [223254.991] ERROR (2177310): Invalid state: WritableStream is locked 14|calling | jobID: "AJ_gicqNNY9CTfo" 14|calling | err: { 14|calling | "type": "TypeError", 14|calling | "message": "Invalid state: WritableStream is locked", 14|calling | "stack": 14|calling | TypeError [ERR_INVALID_STATE]: Invalid state: WritableStream is locked 14|calling | at InternalWritableStream.close (nodeinternal/webstreams/writablestream218:9) 14|calling | at VADStream.endInput (file:///home/AI-Chatbot-Avatar-Developer-india/agent-starter-node/node_modules/.pnpm/@livekit+agents@1.0.11_@livekit+rtc-node@0.13.20/node_modules/@livekit/agents/dist/vad.js14125) 14|calling | at forwardInput (file:///home/AI-Chatbot-Avatar-Developer-india/agent-starter-node/node_modules/.pnpm/@livekit+agents@1.0.11_@livekit+rtc-node@0.13.20/node_modules/@livekit/agents/dist/stt/stream_adapter.js4623) 14|calling | at async Promise.all (index 0) 14|calling | "code": "ERR_INVALID_STATE" 14|calling | } 14|calling | Usage: {"llmPromptTokens":0,"llmPromptCachedTokens":0,"llmCompletionTokens":0,"ttsCharactersCount":0,"sttAudioDurationMs":0}
t
Thanks for reaching out, Jeff. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
c
import { Room, RoomEvent, Track, } from 'livekit-client' in client side after disconnect the in server console i got this error. disconnect and delete the room is good options ?? /root/.pm2/logs/calling-agent-out.log last 15 lines: 14|calling | error: null 14|calling | [223254.991] ERROR (2177310): Invalid state: WritableStream is locked 14|calling | jobID: "AJ_gicqNNY9CTfo" 14|calling | err: { 14|calling | "type": "TypeError", 14|calling | "message": "Invalid state: WritableStream is locked", 14|calling | "stack": 14|calling | TypeError [ERR_INVALID_STATE]: Invalid state: WritableStream is locked 14|calling | at InternalWritableStream.close (nodeinternal/webstreams/writablestream218:9) 14|calling | at VADStream.endInput (file:///home/AI-Chatbot-Avatar-Developer-india/agent-starter-node/node_modules/.pnpm/@livekit+agents@1.0.11_@livekit+rtc-node@0.13.20/node_modules/@livekit/agents/dist/vad.js14125) 14|calling | at forwardInput (file:///home/AI-Chatbot-Avatar-Developer-india/agent-starter-node/node_modules/.pnpm/@livekit+agents@1.0.11_@livekit+rtc-node@0.13.20/node_modules/@livekit/agents/dist/stt/stream_adapter.js4623) 14|calling | at async Promise.all (index 0) 14|calling | "code": "ERR_INVALID_STATE" 14|calling | } 14|calling | Usage: {"llmPromptTokens":0,"llmPromptCachedTokens":0,"llmCompletionTokens":0,"ttsCharactersCount":0,"sttAudioDurationMs":0}
t
I'm glad you reached out about the "Invalid state: WritableStream is locked" error. This usually happens when a WritableStream already has an active writer, and another writer is being created without releasing the first one. Let's work through this together! Here are some steps that can help you resolve this: • Make sure to release the writer lock before creating a new writer. For example:
Copy code
// Release the current writer
if (writer) {
  writer.releaseLock();
}

// Now you can get a new writer
const newWriter = writableStream.getWriter();
• Using a try-finally block is a great way to ensure cleanup every time:
Copy code
try {
  const writer = writableStream.getWriter();
  // Use the writer
  await writer.write(data);
} finally {
  // Always release the lock
  writer.releaseLock();
}
• Also, it's helpful to handle expected errors gracefully and make sure the stream is properly closed when disconnecting or deleting a room. If you'd like, feel free to share more details about your setup or any specific challenges you're facing. I'm here to help you through it! Sources: src/api/WebSocketStream.ts | agents/src/stream/deferred_stream.ts
It looks like the error "Invalid state: WritableStream is locked" happens when a WritableStream is accessed while it’s already locked. This usually occurs if a writer hasn’t been properly released before trying to use it again. To help you resolve this, here are a few friendly tips: • Make sure to release any writer you get from a WritableStream by calling
writer.releaseLock()
once you’re done with it. • Consider wrapping your writer operations inside a
try-finally
block—this way, you can be sure the lock is released even if something unexpected happens. • It’s also a good idea to check the stream’s state before accessing it, to prevent any conflicts. About your question on disconnecting and deleting the room, that’s actually a smart way to clean things up. Just remember to properly close and release all streams and writers before disconnecting, so you can avoid running into this error. If you’d like, feel free to share more details about your setup or any other questions you have—I’m here to help!
c
still same issue
t
I understand how frustrating the "WritableStream is locked" error can be, especially when cleanup during disconnection and room deletion isn't fully handled. Let’s work through some additional steps together to help resolve this: 1. Ensure Proper Cleanup • Make sure to release all active writer locks before closing streams:
Copy code
if (writer) {
  writer.releaseLock();
}
• Then, close the streams after releasing those locks:
Copy code
await writableStream.close();
2. Handle Errors Gracefully • It’s helpful to use error detection utilities to catch and manage stream-related errors smoothly:
Copy code
export function isStreamReaderReleaseError(e) {
  const allowedMessages = [
    'Invalid state: Releasing reader',
    'Invalid state: The reader is not attached to a stream',
  ];

  if (e instanceof TypeError) {
    return allowedMessages.some((message) => e.message.includes(message));
  }

  return false;
}
3. Follow Proper Disconnection Sequence • Be sure the room disconnects properly before you delete it, like this:
Copy code
room.disconnect();
4. Server-Side Room Deletion • When you delete a room on the server side, all participants will be disconnected automatically, which helps trigger cleanup on the client side. Please feel free to reach out if you want me to walk through your specific implementation or if these steps don’t fully resolve the issue. I’m here to help! Sources: lib/src/participant/local.dart | Managing rooms | LiveKit Docs