-
Notifications
You must be signed in to change notification settings - Fork 12
fix: Remove sys___init requirement - sessions always auto-created #831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -846,48 +846,35 @@ func (us *UnifiedServer) ensureSessionDirectory(sessionID string) error { | |
| } | ||
|
|
||
| // requireSession checks that a session has been initialized for this request | ||
| // When DIFC is disabled (default), automatically creates a session if one doesn't exist | ||
| // Sessions are automatically created if one doesn't exist (for standard MCP client compatibility) | ||
| func (us *UnifiedServer) requireSession(ctx context.Context) error { | ||
| sessionID := us.getSessionID(ctx) | ||
| log.Printf("Checking session for ID: %s", sessionID) | ||
|
|
||
| // If DIFC is disabled (default), use double-checked locking to auto-create session | ||
| if !us.enableDIFC { | ||
| us.sessionMu.RLock() | ||
| session := us.sessions[sessionID] | ||
| us.sessionMu.RUnlock() | ||
|
|
||
| if session == nil { | ||
| // Need to create session - acquire write lock | ||
| us.sessionMu.Lock() | ||
| // Double-check after acquiring write lock to avoid race condition | ||
| if us.sessions[sessionID] == nil { | ||
| log.Printf("DIFC disabled: auto-creating session for ID: %s", sessionID) | ||
| us.sessions[sessionID] = NewSession(sessionID, "") | ||
| log.Printf("Session auto-created for ID: %s", sessionID) | ||
|
|
||
| // Ensure session directory exists in payload mount point | ||
| // This is done after releasing the lock to avoid holding it during I/O | ||
| us.sessionMu.Unlock() | ||
| if err := us.ensureSessionDirectory(sessionID); err != nil { | ||
| logger.LogWarn("client", "Failed to create session directory for session=%s: %v", sessionID, err) | ||
| // Don't fail - payloads will attempt to create the directory when needed | ||
| } | ||
| return nil | ||
| } | ||
| us.sessionMu.Unlock() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // DIFC is enabled - require explicit session initialization | ||
| // Use double-checked locking to auto-create session if needed | ||
| us.sessionMu.RLock() | ||
| session := us.sessions[sessionID] | ||
| us.sessionMu.RUnlock() | ||
|
|
||
| if session == nil { | ||
| log.Printf("Session not found for ID: %s. Available sessions: %v", sessionID, us.getSessionKeys()) | ||
| return fmt.Errorf("sys___init must be called before any other tool calls") | ||
| // Need to create session - acquire write lock | ||
| us.sessionMu.Lock() | ||
| // Double-check after acquiring write lock to avoid race condition | ||
| if us.sessions[sessionID] == nil { | ||
| log.Printf("Auto-creating session for ID: %s", sessionID) | ||
| us.sessions[sessionID] = NewSession(sessionID, "") | ||
| log.Printf("Session auto-created for ID: %s", sessionID) | ||
|
|
||
| // Ensure session directory exists in payload mount point | ||
| // This is done after releasing the lock to avoid holding it during I/O | ||
| us.sessionMu.Unlock() | ||
| if err := us.ensureSessionDirectory(sessionID); err != nil { | ||
| logger.LogWarn("client", "Failed to create session directory for session=%s: %v", sessionID, err) | ||
| // Don't fail - payloads will attempt to create the directory when needed | ||
| } | ||
| return nil | ||
|
Comment on lines
859
to
+875
|
||
| } | ||
| us.sessionMu.Unlock() | ||
| } | ||
|
|
||
| log.Printf("Session validated for ID: %s", sessionID) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requireSession()now auto-creates sessions and callsensureSessionDirectory(sessionID)based on the session ID from context. SinceensureSessionDirectory()usesfilepath.Join(us.payloadDir, sessionID)without validating/sanitizingsessionID, a malicious or malformed session ID containing path separators,.., or an absolute path could cause directory creation outside the intended payload directory. Please validate the session ID before using it in filesystem paths (e.g., reject absolute paths, clean and ensure the result stays withinus.payloadDir, and/or restrict to a safe character set).