Skip to content

Close MCP auditor log file on server stop - #7373

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

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

Conversation

@bharathvemula

@bharathvemula bharathvemula commented Nov 28, 2026 •

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.
  • Rework (review feedback): Handler now builds the auditor only when the Server does not already have one, so a repeat call shares the first auditor instead of opening the log file again; Stop clears the retained auditor after a successful close. The regression test keeps the returned handler alive and no longer asserts on the unexported field or the file's existence.

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)

Ran the Taskfile's test flags on the touched package: go test -ldflags=-extldflags=-Wl,-w -race -count=1 ./pkg/vmcp/server/ — full package suite passes (166.666s). 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 (verified by temporarily disabling the close: the test fails with "workflow audit file descriptor must be released"). A repeat-Handler check (two calls, then Stop) also passes with no descriptor leak. golangci-lint run --allow-parallel-runners ./pkg/vmcp/server/...: 0 issues; gofmt and go vet clean. The whole-repo task test / task lint-fix targets were not run to completion on this machine (the lint run was killed by the VM's memory limit; the full test run was stopped early) — the boxes above are left unticked accordingly.

Special notes for reviewers

  • Handler is documented as safe to call multiple times; on a repeat call the first auditor is shared 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 predates the AuditSubjectEnricher option (Let vMCP embedders add audit event subjects #7135). The reworked Handler conflicts with that change on merge, so a rebase onto current upstream main is needed before this can land; the rebased version (nil-guard plus the WithSubjectEnricher argument) is verified locally and will be pushed once the fork is synced.

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 <12749607+bharathvemula@users.noreply.github.com>
@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.

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up, Bharath. The core of the change is right: keeping the auditor on the Server and closing it in Stop, after the HTTP shutdown, is what the issue asked for, and the close error is joined with the others like the sibling code does.

First, a process note. Our contributing guide asks you to comment on an issue and wait for a maintainer to assign it before starting work. #7372 was a few hours old, unassigned, and filed by a maintainer who may have had plans for it, so it's worth checking first. It saves you from writing a fix someone else is about to push. Please do that next time.

I also want to say something about AI-assisted PRs, since you disclosed it (thanks for that). Generating a fix is the cheap part now. Almost anyone can get a plausible diff out of a model in minutes, so the diff alone isn't what helps the project. What helps is that someone has read the surrounding code, understood why it's shaped the way it is, run the checks the way the project runs them, and can stand behind every line and every claim in the description. A few things here suggest that last step was skipped:

  • The description says a repeat Handler call "reuses the retained auditor instead of opening the log file again". The code does the opposite. It builds a second auditor, which opens the file again, and then closes it. Your commit message describes it correctly, so the two disagree.
  • The test plan ticks task test and task lint-fix, but the text says you ran go test and golangci-lint run directly. We ask for the task targets because they carry the right flags, so please run those and tick only what you ran.
  • That repeat-call branch is never exercised in production, since nothing calls Handler twice, and it isn't tested either. It's extra code for a case that doesn't happen.

Changes I'd like before this merges:

  1. In Handler, only build the auditor when s.mcpAuditor == nil, rather than opening a file and closing it again. Please drop the comment about upstream merging cleanly. That's about your branch, not the code. Also update the Handler doc comment, since it now writes s.mcpAuditor and later calls share the first auditor.
  2. Tighten the test:
    • Keep the handler it gets back from Handler alive. As written the leaked *os.File can be finalised by the GC, so the test might pass without the fix.
    • The os.Stat check proves nothing, because New already opens that file through the core's workflow auditor.
    • Asserting on the unexported srv.mcpAuditor ties the test to the implementation. The file-descriptor check already covers the behaviour.
  3. Please fix the description and the test-plan checkboxes as above.

A small nit, optional: set s.mcpAuditor = nil after a successful close, so a second Stop doesn't return a "file already closed" error.

Happy to help if any of this is unclear. Thanks again for the work.

Build the MCP auditor only when the Server does not already have one, so a repeat Handler call shares the first auditor instead of opening the log file again and closing the surplus. Update the Handler doc comment to say the first call builds the auditor and later calls share it. Clear the retained auditor after a successful close in Stop so a second Stop does not report a file-already-closed error.

Tighten TestStopClosesMCPAuditorLogFile: keep the returned handler alive so the auditor it references cannot be finalised by the GC before Stop runs, and drop the os.Stat check and the assertion on the unexported mcpAuditor field — the file-descriptor check covers the behaviour.

Signed-off-by: Bharath Vemula <12749607+bharathvemula@users.noreply.github.com>
@ChrisJBurns

Copy link
Copy Markdown
Collaborator

Approved! But there's some merge conflicts that will need to be resolved @bharathvemula

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

3 participants