Skip to content

[bug] Potential session.Store resource leak #2049

@docker-agent

Description

@docker-agent

🟡 medium - bug

File: cmd/root/run.go (line 269)

Code

sessStore, err := session.NewSQLiteSessionStore(sessionDB)
if err != nil {
	return nil, nil, fmt.Errorf("creating session store: %w", err)
}

// Create model switcher config for runtime model switching support
modelSwitcherCfg := &runtime.ModelSwitcherConfig{
	Models:             loadResult.Models,
	Providers:          loadResult.Providers,
	ModelsGateway:      f.runConfig.ModelsGateway,
	EnvProvider:        f.runConfig.EnvProvider(),
	AgentDefaultModels: loadResult.AgentDefaultModels,
}

localRt, err := runtime.New(t,
	runtime.WithSessionStore(sessStore),
	runtime.WithCurrentAgent(f.agentName),
	runtime.WithTracer(otel.Tracer(AppName)),
	runtime.WithModelSwitcherConfig(modelSwitcherCfg),
)

Problem

The session.Store created by session.NewSQLiteSessionStore(sessionDB) is passed to runtime.New but is not explicitly closed in createLocalRuntimeAndSession. The localRt (which receives sessStore) is eventually closed by defer rt.Close() in runOrExec. If rt.Close() does not internally close the session.Store, this would lead to an unclosed database connection and a resource leak.

Suggested Fix

Ensure that the runtime.Runtime implementation's Close() method explicitly closes the session.Store it was initialized with. Alternatively, the session.Store should be closed in createLocalRuntimeAndSession (e.g., using a defer statement) or the ownership of the session.Store should be clearly defined to ensure proper cleanup. If runtime.New takes ownership and ensures closure, then this is not an issue, but it's not immediately apparent from the current code.


Found by nightly codebase scan

Metadata

Metadata

Assignees

No one assigned

    Labels

    automatedIssues created by cagentkind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions