Skip to content

Commit b36b0cd

Browse files
committed
Clean up orphaned locks on session creation failure
Clarify getOrCreateSession JSDoc to explain why callers must hold the session lock. Add cleanup of sessionLocks when session creation fails to prevent minor memory leaks.
1 parent d522cc3 commit b36b0cd

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/sandbox-container/src/services/session-manager.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ export class SessionManager {
4747
*
4848
* Uses a two-phase approach:
4949
* 1. Check if session exists (fast path)
50-
* 2. Use creatingLocks map with atomic check-and-set to coordinate creation
50+
* 2. Use creatingLocks map to coordinate creation across callers
5151
*
52-
* IMPORTANT: This method must be called while holding the session lock
53-
* to prevent race conditions between checking and setting creatingLocks.
52+
* IMPORTANT: All callers (executeInSession, withSession, etc.) acquire the
53+
* session lock before calling this method. The lock ensures only one caller
54+
* executes this method at a time for a given sessionId, making the
55+
* creatingLocks check-and-set atomic.
5456
*/
5557
private async getOrCreateSession(
5658
sessionId: string,
@@ -125,6 +127,10 @@ export class SessionManager {
125127
};
126128
} finally {
127129
this.creatingLocks.delete(sessionId);
130+
// Clean up orphaned lock if session creation failed
131+
if (!this.sessions.has(sessionId)) {
132+
this.sessionLocks.delete(sessionId);
133+
}
128134
}
129135
}
130136

0 commit comments

Comments
 (0)