Skip to content

Close MCP auditor log file on server stop - #7373

Open
bharathvemula wants to merge 1 commit into
stacklok:mainfrom
bharathvemula:fix/7372-close-mcp-auditor-log-file
Open

bharathvemula wants to merge 1 commit into
stacklok:mainfrom
bharathvemula:fix/7372-close-mcp-auditor-log-file

Conversation

@bharathvemula

Copy link
Copy Markdown
Contributor

Summary

  • Server.Handler builds the MCP auditor with audit.NewAuditorWithTransport when AuditConfig is set, but only its middleware was kept, so Server.Stop never closed the auditor's log writer. Each server built with an audit log file leaked one file descriptor per build/stop cycle, which matters for embedders that build and stop vMCP servers repeatedly.
  • Retain the auditor on the Server when Handler creates it, and close it in Stop after the HTTP server has shut down, next to the session data storage close, so no in-flight request writes to a closed log file. A close error is reported with the other shutdown errors.
  • This follows the WorkflowAuditor fix in Close workflow audit log writers #6110, including the os.Stdout/os.Stderr guard already in Auditor.Close, so a stdout-backed auditor is never closed.

Fixes #7372

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Verified with the Taskfile's test flags on the touched package: go test -ldflags=-extldflags=-Wl,-w -race -count=1 ./pkg/vmcp/server/ — full package passes (254.967s at upstream HEAD; targeted audit tests re-run and passing after the rebase onto the fork's main). New regression test TestStopClosesMCPAuditorLogFile builds a server with an audit log file, calls Handler, stops it, and asserts via /proc/self/fd that no descriptor to the log file remains; it failed before the Stop close was added and passes with it. golangci-lint run ./pkg/vmcp/server/...: 0 issues; gofmt and go vet clean.

Special notes for reviewers

  • Handler is documented as safe to call multiple times; on a repeat call the retained auditor is reused instead of opening the log file again.
  • If Start fails after Handler has built the auditor (listener creation or status reporter start), the auditor is released only when the caller calls Stop; that path is unchanged by this fix and could be a follow-up if maintainers want it.
  • The branch is based on the fork's main (Nov 10), which is behind upstream main; the PR diff is the single fix commit only. The fork's main predates the AuditSubjectEnricher option, so this version calls NewAuditorWithTransport without it — a rebase onto current upstream main would pick that option up with no other change.

AI assistance disclosure: this PR was prepared with the help of an AI coding assistant; the author reviewed the changes and ran the tests above.

Server.Handler builds the MCP auditor with audit.NewAuditorWithTransport when AuditConfig is set, but only its middleware was kept, so Server.Stop never closed the log writer and each build/stop cycle leaked one file descriptor. Retain the auditor on the Server when Handler creates it and close it in Stop after the HTTP server has shut down, next to the session data storage close, following the WorkflowAuditor fix in stacklok#6110. Handler keeps the first auditor it creates; on repeat calls the surplus auditor is closed immediately so its log file is not leaked. The auditor creation call is left byte-identical to upstream so the WithSubjectEnricher argument added on main merges cleanly.

Signed-off-by: Bharath Vemula <[email protected]>
@bharathvemula
bharathvemula force-pushed the fix/7372-close-mcp-auditor-log-file branch from 73f9680 to d376166 Compare November 28, 2026 23:59
@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.64%. Comparing base (d702679) to head (d376166).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7373      +/-   ##
==========================================
+ Coverage   80.93%   82.64%   +1.71%     
==========================================
  Files         867      934      +67     
  Lines       88888    97050    +8162     
==========================================
+ Hits        71939    80207    +8268     
+ Misses      16944    16838     -106     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vmcp: server never closes its MCP auditor's log file

1 participant