Skip to content

Session create leaves a saved session and its engine behind when the broker commit fails #2972

Backlog

Description

@jhrozek

Problem

When a session is created with the MCP broker on, createPerSessionEngine saves the session before it commits the session's broker attachment. If that commit fails, the create returns an error, but the saved session stays behind:

  1. The engine is installed in sessionEngines and the session record is saved by persistCreatedSession (internal/adapter/server/service.go:3373 on the PR feat(identity): give named-definition sessions their servers' gateway tools #2965 branch, :3348 on main).
  2. commitBrokerAttachment fails (service.go:3411, :3386 on main).
  3. The function returns nil, ErrInternal. The deferred finalizeBrokerAttachment aborts the attachment.

Result:

  • The session record exists and carries an ExternalBinding for an attachment that was aborted.
  • Its engine is still registered in sessionEngines.
  • The caller gets an error and no session ID, so it cannot tell that the session exists, or clean it up.

What a retry with the same request ID, or a later call on that session, then sees has not been checked.

The other paths already handle this

The two other places that commit a broker attachment after publishing clean up when the commit fails:

  • Fork/Clear (placement_successor.go:489-495): calls cleanupEngine() and returns the saved ID with the error ("Saved already: report the ID with the error").
  • Rebuild (service.go:7505-7515): restores the prior engine, or deletes the new one.

The create path should do the same: remove the engine it installed, and either return the saved session's ID with the error or make the saved record recoverable (for example, remove it or mark it so a retry can finish the commit).

Scope

This happens on main today for every broker-attached session create. PR #2965 also routes named-definition sessions (agent_definition_name) through this path, so they are affected too once it merges.

Test

Inject a broker Commit failure after persistCreatedSession succeeds. Assert that the engine is gone from sessionEngines, and assert what a retry with the same request ID and a GetSession call see. A factory failure does not exercise this path, because it fails before the save.

Found while reviewing #2965.

Activity

  1. ningsiyuan1024 commented on Nov 25, 2026

    @ningsiyuan1024

    I'd like to work on this. I'll inject a broker commit failure after the session is saved, then check engine cleanup and what GetSession and a retry with the same request ID see. Could you assign this to me if available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions