Skip to content

fix(logs): release the per-server log sink on every Disconnect - #1268

Merged
Dumbris merged 2 commits into
mainfrom
fix/1266-close-upstream-log-writer
Sep 12, 2026
Merged

Dumbris merged 2 commits into
mainfrom
fix/1266-close-upstream-log-writer

Conversation

@Dumbris

@Dumbris Dumbris commented Sep 12, 2026

Copy link
Copy Markdown
Member

Closes #1266

Problem

The per-server upstream logger's lumberjack sink was wrapped in zapcore.AddSync and never closed, so a core held one open file handle per server it had ever logged for, for the life of the process. On Windows an open handle also pins the file: since 2026-09-07 every internal/server test that spins up a Server failed CI's t.TempDir() cleanup on server-<name>.log, turning the Unit Tests and E2E Tests workflows red on every push to main (they only run their Windows leg on main, so PRs never saw it).

Fix

  • internal/logs: the file sink is a rotatingSink (lumberjack + Sync) that is also an io.Closer. NewUpstreamServerLogger returns the closer; CreateUpstreamServerLogger keeps its shape for callers that cannot own one.
  • core.Client stores the closer and closes it at the end of DisconnectWithContext — the one path RemoveServer, ShutdownAll and a reconnect all reach. lumberjack reopens the file on the next Write, so a reconnecting server logs on unchanged.

Testing

  • TestNewUpstreamServerLogger_CloserReleasesTheFileAndWritesReopen: write → close → rename succeeds → write reopens (the rename is the real check on the Windows leg).
  • TestDisconnect_ClosesTheUpstreamLogSink: Disconnect closes the sink, every time.
  • Verified the actual leak with lsof from an internal/server fixture: after Server.Shutdown() no fd points into the log dir; with closeUpstreamLog removed the server-*.log handle is still open.
  • go test -race on internal/logs internal/upstream/... internal/runtime and internal/server (CI skip regex) green; GOOS=windows go build ./... and go vet clean; golangci-lint v2 0 issues.
  • opencode astra review: two test-side leaks it found are fixed in the second commit. One residual it raised is left as a follow-up: a stderr-monitor goroutine abandoned after StopStderrMonitoring's 500 ms wait could log a late EOF and reopen the sink after Disconnect returns. That is pre-existing and does not affect the failing fixtures (their clients never connect); a terminal write fence for RemoveServer/ShutdownAll would close it fully.

🤖 Generated with Claude Code

Dumbris and others added 2 commits September 12, 2026 14:18
The upstream server logger's lumberjack sink was wrapped in
zapcore.AddSync and never closed, so a core held one open file handle
per server it had ever logged for, for the life of the process. On
Windows that also pins the file: since 2026-09-07 every internal/server
test that spins up a Server failed in CI's t.TempDir() cleanup on
server-<name>.log, turning the Unit Tests and E2E workflows red on
every push to main (#1266).

The file sink now doubles as an io.Closer, NewUpstreamServerLogger
returns it, and core.Client closes it at the end of Disconnect — the
one path RemoveServer, ShutdownAll and a reconnect all reach. lumberjack
reopens the file on the next write, so a reconnecting server logs on
unchanged. CreateUpstreamServerLogger keeps its shape for callers that
cannot own the closer.

Verified with lsof: after Server.Shutdown no fd points into the log
dir; with closeUpstreamLog removed the server-*.log handle is still
open.

Closes #1266

Co-Authored-By: Claude Opus 5 <[email protected]>
… guard

Cross-model review: the compatibility test wrote through the closer-less
API (opening a file it could never close), and the Disconnect test
swapped in a stub closer while the real sink stayed open. Both would
have failed Windows CI's TempDir cleanup themselves. The former no
longer writes; the latter's stub delegates to the real closer.

Co-Authored-By: Claude Opus 5 <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 26df234
Status: ✅  Deploy successful!
Preview URL: https://fc4e3a3a.mcpproxy-docs.pages.dev
Branch Preview URL: https://fix-1266-close-upstream-log.mcpproxy-docs.pages.dev

View logs

@codecov-commenter

codecov-commenter commented Sep 12, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 82.14286% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/logs/logger.go 83.33% 3 Missing ⚠️
internal/upstream/core/connection_lifecycle.go 71.42% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/1266-close-upstream-log-writer

Available Artifacts

  • archive-darwin-amd64 (29 MB)
  • archive-darwin-arm64 (27 MB)
  • archive-linux-amd64 (17 MB)
  • archive-linux-arm64 (16 MB)
  • archive-windows-amd64 (29 MB)
  • archive-windows-arm64 (26 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (24 MB)
  • installer-dmg-darwin-arm64 (21 MB)
  • smart-mcp-proxymcpproxy-goNJ2N85.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 34691092434 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris
Dumbris merged commit dc84174 into main Sep 12, 2026
40 checks passed
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.

Windows unit tests red on main since 2026-09-07: per-server log writer is never closed

2 participants