You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
vmcp: server never closes its MCP auditor's log file #7372
Server.Handler (pkg/vmcp/server/server.go) builds the MCP auditor with audit.NewAuditorWithTransport when AuditConfig is set. With AuditConfig.LogFile set, that opens the file through Config.GetLogWriter. The auditor is a local variable: only its middleware is kept, and Server.Stop never calls Auditor.Close.
So each server built with an audit log file keeps one file descriptor open after it stops. A process that builds and stops vMCP servers repeatedly, such as an embedder, leaks one descriptor per build.
WorkflowAuditor had the same leak, fixed in #6094. The MCP auditor was not covered.
Proposal
Keep 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. Closing after shutdown means no in-flight request writes to a closed file.
Done when
Stop closes the MCP auditor's log writer when one was opened, and reports a close error with the others.
A test builds a server with an audit log file, stops it, and checks the auditor was closed.
Context
Mecatl embeds the vMCP with an audit log file and builds one server per process build, so it hits this.
The leak is real and "close it after the HTTP server has shut down" is the right ordering. Three things decide where the code goes, and the third one changes the answer.
Handler already promises to be callable more than once
Handler's doc block says so in as many words (pkg/vmcp/server/server.go:714): "Each call builds a fresh handler. The method is safe to call multiple times. All returned handlers share the same underlying MCPServer and SessionManager, so callers should not serve concurrent traffic through multiple handlers."
An assignment like s.auditor = auditor inside Handler therefore has no defined meaning on the second call — the field holds whichever auditor was built last, and the previously stored one becomes unreachable while still holding an open *os.File. That is a leak the field was supposed to fix. Storing a list, or storing it only from Start, are the only two shapes consistent with the existing contract.
Start is the caller that has a Stop
Start calls Handler at :963; shutdownFuncs is appended at :1045, :1057, :1075, and its field comment (:409-411) says it is "Populated during Start() initialization before blocking … Stop() is only called after Start's select returns". Stop's doc (:1093-1100) and the Handler doc (:717 region) both say embedders using Handler must drain and stop their own server — there is no promise that Stop follows a bare Handler call.
So Start owning the auditor satisfies the "Done when" for every path Stop is legal on, and it keeps Handler stateless. Concretely: build the auditor in Start, wrap the handler it gets back, register auditor.Close into shutdownFuncs next to the three existing entries, and pass nil audit config into the Handler construction so Handler does not build a second one. The risk to watch is that Handler is exported and a Handler-only embedder then gets no auditor at all — that is a behaviour change worth one sentence in the PR description rather than a silent difference.
If it stays in Handler, Start has two exits that never reach Stop
Two returns after Handler succeeds, before shutdownFuncs is populated:
:995-997 — startDiagnostics failed, return err;
:1000-1010 — net.Listen failed; this path already unwinds diagnostics by hand at :1005, which is the pattern to copy.
Both would leak a Handler-built auditor with no Stop to come. Whichever object owns the auditor needs the same treatment on those two paths, and it cannot be half of each.
The double-close is benign but must stay that way
Auditor.Close → closeLogWriter (pkg/audit/auditor.go:101-117) returns nil for os.Stdout/os.Stderr, so the default path is safe; with LogFile set it closes the *os.File, and a second Close on a closed *os.File returns os.ErrClosed. Stop may legally be retried after an incomplete drain (:1096-1098), and the retry re-runs the shutdownFuncs loop at :1132 — so without a sync.Once (or a nil-the-field after close) a legal retry surfaces os.ErrClosed as a shutdown failure. Worth a comment, since it is the one place where the issue's "reports a close error with the others" instruction interacts badly with an existing contract.
One more: Config.GetLogWriter (pkg/audit/config.go:70-82) opens with O_CREATE|O_WRONLY|O_APPEND and returns os.Stdout only when LogFile is empty. The issue's "when one was opened" is therefore exactly AuditConfig.LogFile != "", and the test should set that — a test that does not will exercise the os.Stdout branch and pass with the bug still present.
Two assertions that fail when the fix regresses
"Checks the auditor was closed" is satisfiable by a nil field or a mock. Instead:
retain the writer from Config.GetLogWriter, stop the server, then write one more audit event and require an error — a closed *os.File returns os.ErrClosed, a dropped reference does not;
count os.ReadDir("/proc/self/fd") before building and after stopping, require a return to baseline. This is the property the issue is about, and it also catches a second Handler build.
Pair both with a Stop-twice case, since the doc explicitly permits a serial retry and that is what a sync.Once would paper over.
Activity
01men commentedon Dec 2, 2026
The leak is real and "close it after the HTTP server has shut down" is the right ordering. Three things decide where the code goes, and the third one changes the answer.
Handleralready promises to be callable more than onceHandler's doc block says so in as many words (pkg/vmcp/server/server.go:714): "Each call builds a fresh handler. The method is safe to call multiple times. All returned handlers share the same underlying MCPServer and SessionManager, so callers should not serve concurrent traffic through multiple handlers."An assignment like
s.auditor = auditorinsideHandlertherefore has no defined meaning on the second call — the field holds whichever auditor was built last, and the previously stored one becomes unreachable while still holding an open*os.File. That is a leak the field was supposed to fix. Storing a list, or storing it only fromStart, are the only two shapes consistent with the existing contract.Startis the caller that has aStopStartcallsHandlerat:963;shutdownFuncsis appended at:1045,:1057,:1075, and its field comment (:409-411) says it is "Populated during Start() initialization before blocking … Stop() is only called after Start's select returns".Stop's doc (:1093-1100) and theHandlerdoc (:717region) both say embedders usingHandlermust drain and stop their own server — there is no promise thatStopfollows a bareHandlercall.So
Startowning the auditor satisfies the "Done when" for every pathStopis legal on, and it keepsHandlerstateless. Concretely: build the auditor inStart, wrap the handler it gets back, registerauditor.CloseintoshutdownFuncsnext to the three existing entries, and passnilaudit config into theHandlerconstruction soHandlerdoes not build a second one. The risk to watch is thatHandleris exported and aHandler-only embedder then gets no auditor at all — that is a behaviour change worth one sentence in the PR description rather than a silent difference.If it stays in
Handler,Starthas two exits that never reachStopTwo returns after
Handlersucceeds, beforeshutdownFuncsis populated::995-997—startDiagnosticsfailed,return err;:1000-1010—net.Listenfailed; this path already unwinds diagnostics by hand at:1005, which is the pattern to copy.Both would leak a
Handler-built auditor with noStopto come. Whichever object owns the auditor needs the same treatment on those two paths, and it cannot be half of each.The double-close is benign but must stay that way
Auditor.Close→closeLogWriter(pkg/audit/auditor.go:101-117) returns nil foros.Stdout/os.Stderr, so the default path is safe; withLogFileset it closes the*os.File, and a secondCloseon a closed*os.Filereturnsos.ErrClosed.Stopmay legally be retried after an incomplete drain (:1096-1098), and the retry re-runs theshutdownFuncsloop at:1132— so without async.Once(or a nil-the-field after close) a legal retry surfacesos.ErrClosedas a shutdown failure. Worth a comment, since it is the one place where the issue's "reports a close error with the others" instruction interacts badly with an existing contract.One more:
Config.GetLogWriter(pkg/audit/config.go:70-82) opens withO_CREATE|O_WRONLY|O_APPENDand returnsos.Stdoutonly whenLogFileis empty. The issue's "when one was opened" is therefore exactlyAuditConfig.LogFile != "", and the test should set that — a test that does not will exercise theos.Stdoutbranch and pass with the bug still present.Two assertions that fail when the fix regresses
"Checks the auditor was closed" is satisfiable by a nil field or a mock. Instead:
Config.GetLogWriter, stop the server, then write one more audit event and require an error — a closed*os.Filereturnsos.ErrClosed, a dropped reference does not;os.ReadDir("/proc/self/fd")before building and after stopping, require a return to baseline. This is the property the issue is about, and it also catches a secondHandlerbuild.Pair both with a
Stop-twice case, since the doc explicitly permits a serial retry and that is what async.Oncewould paper over.