Skip to content

fix: flush and sync all router outputs before closing any - #17

Merged
ssgreg merged 1 commit into
mainfrom
fix/router-close-once
Dec 4, 2026
Merged

ssgreg merged 1 commit into
mainfrom
fix/router-close-once

Conversation

@ssgreg

@ssgreg ssgreg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A writer can be owned by one route and only written by another:

f, _ := os.Create("app.log")
_, closeFn, _ := logf.NewRouter().
	Route(logf.JSON().Build(), logf.OutputCloser(logf.LevelDebug, f)).
	Route(logf.Text().Build(), logf.Output(logf.LevelInfo, f)).
	Build()
err := closeFn() // "sync app.log: file already closed"

The close function flushed, synced and closed each output in turn. The first output closed f, the second synced it again, and close returned an error although every entry had been written. With the routes in the other order the same setup closed cleanly.

Change

  • Close flushes and syncs every output first, then closes the writers passed to OutputCloser.
  • The OutputCloser doc says that a writer shared by several routes is closed once per OutputCloser, so it should go to OutputCloser once and to Output elsewhere.

Testing

TestRouterCloseSyncsSharedWriterBeforeClosingIt builds the setup above on a real file and expects close to return nil and the file to hold both lines. On main it fails with file already closed.

go test -race ./... and golangci-lint run pass. The write path is unchanged; RealisticPipeline, six interleaved rounds against main, shows no statistically significant change in time, B/op or allocs/op.

A writer can be owned by one route through OutputCloser and also written by another route through Output. The close function used to flush, sync and close each output in turn, so when the owning route came first the writer was closed and then synced again, and close reported a "file already closed" error although everything had been written. Close now flushes and syncs every output first and closes the owned writers after that. The OutputCloser documentation now says that a shared writer is closed once per OutputCloser.
@ssgreg
ssgreg force-pushed the fix/router-close-once branch from 36a90d9 to faa32b1 Compare December 4, 2026 06:33
@ssgreg ssgreg changed the title fix: close a writer shared by router outputs once fix: flush and sync all router outputs before closing any Dec 4, 2026
@ssgreg

ssgreg commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Reworked: dropped the pointer-identity dedupe of shared writers and the stored close result. Ownership is already explicit through OutputCloser and Output, and a repeated close has no promised result. The PR now only flushes and syncs every output before closing any, which removes a spurious file already closed when the owning route comes first, and documents that a shared writer goes to OutputCloser once. The description is updated.

@ssgreg
ssgreg merged commit 6a9766f into main Dec 4, 2026
6 checks passed
@ssgreg
ssgreg deleted the fix/router-close-once branch December 4, 2026 06:40
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.

1 participant