Repository navigation
Close MCP auditor log file on server stop - #7373
bharathvemula wants to merge 2 commits into
Conversation
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>
73f9680 to
d376166
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
jhrozek
left a comment
There was a problem hiding this comment.
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
Handlercall "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 testandtask lint-fix, but the text says you rango testandgolangci-lint rundirectly. We ask for thetasktargets 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
Handlertwice, and it isn't tested either. It's extra code for a case that doesn't happen.
Changes I'd like before this merges:
- In
Handler, only build the auditor whens.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 theHandlerdoc comment, since it now writess.mcpAuditorand later calls share the first auditor. - Tighten the test:
- Keep the handler it gets back from
Handleralive. As written the leaked*os.Filecan be finalised by the GC, so the test might pass without the fix. - The
os.Statcheck proves nothing, becauseNewalready opens that file through the core's workflow auditor. - Asserting on the unexported
srv.mcpAuditorties the test to the implementation. The file-descriptor check already covers the behaviour.
- Keep the handler it gets back from
- 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>
|
Approved! But there's some merge conflicts that will need to be resolved @bharathvemula |
Summary
Server.Handlerbuilds the MCP auditor withaudit.NewAuditorWithTransportwhenAuditConfigis set, but only its middleware was kept, soServer.Stopnever 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.ServerwhenHandlercreates it, and close it inStopafter 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.WorkflowAuditorfix in Close workflow audit log writers #6110, including theos.Stdout/os.Stderrguard already inAuditor.Close, so a stdout-backed auditor is never closed.Handlernow builds the auditor only when theServerdoes not already have one, so a repeat call shares the first auditor instead of opening the log file again;Stopclears 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
Test plan
task test)task test-e2e)task lint-fix)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 testTestStopClosesMCPAuditorLogFilebuilds a server with an audit log file, callsHandler, stops it, and asserts via/proc/self/fdthat no descriptor to the log file remains; it failed before theStopclose 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-Handlercheck (two calls, thenStop) also passes with no descriptor leak.golangci-lint run --allow-parallel-runners ./pkg/vmcp/server/...: 0 issues;gofmtandgo vetclean. The whole-repotask test/task lint-fixtargets 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
Handleris documented as safe to call multiple times; on a repeat call the first auditor is shared instead of opening the log file again.Startfails afterHandlerhas built the auditor (listener creation or status reporter start), the auditor is released only when the caller callsStop; that path is unchanged by this fix and could be a follow-up if maintainers want it.main(Nov 10), which predates theAuditSubjectEnricheroption (Let vMCP embedders add audit event subjects #7135). The reworkedHandlerconflicts with that change on merge, so a rebase onto current upstreammainis needed before this can land; the rebased version (nil-guard plus theWithSubjectEnricherargument) 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.