Skip to content

fix: panic instead of hanging when a closed SlabWriter or router is used - #11

Open
ssgreg wants to merge 1 commit into
mainfrom
fix/lifecycle
Open

ssgreg wants to merge 1 commit into
mainfrom
fix/lifecycle

Conversation

@ssgreg

@ssgreg ssgreg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Needs #17 (merged), which flushes and syncs every router output before closing any. Without it, a SlabWriter given to OutputCloser in one route and to Output in a later one would now panic on Flush during close.

Problem

Writing to a SlabWriter after Close neither panics nor fails:

sw := logf.NewSlabWriter(&out).SlabSize(16).SlabCount(2).Build()
sw.Close()
for i := 0; i < 10; i++ {
	sw.Write([]byte("0123456789abcdef"))   // hangs on the third write
}

The I/O goroutine is gone, so filled slabs are never returned. Once the pool is empty, Write waits for a free slab forever while holding the writer's mutex, and every goroutine that logs after that hangs too. With DropOnFull the same entries are dropped without a trace. A router keeps accepting entries after its close function ran.

Change

  • SlabWriter.Write and SlabWriter.Flush panic after Close. The check reads a flag under the mutex Write already takes, so the hot path gets no new synchronization. A shutdown ordering bug now shows up as a stack trace pointing at the late writer.
  • The router's Handle panics after close. This is one atomic load per entry.
  • The Build doc says to stop logging before calling close: the flag catches a Handle that starts after close, it does not wait for one already in progress. No lock is added to the per-entry path.

Testing

New tests in lifecycle_test.go:

  • TestSlabWriterWriteAfterClosePanics, blocking and DropOnFull modes. main: blocks, fails after the 5 s guard. Branch: panics with logf: SlabWriter.Write after Close.
  • TestSlabWriterFlushAfterClosePanics. main: no panic.
  • TestRouterHandleAfterClosePanics. main: the entry is written.
  • TestRouterCloseFlushesSharedSlabWriterBeforeClosingIt: one SlabWriter in OutputCloser of the first route and Output of the second; close returns nil and both entries reach the output. It guards the new Flush panic against the close order.

go test -race ./... and golangci-lint run (v2, repo config) pass.

Benchmarks were run on the previous revision of this branch (6fe4158 against main at bea5187). SlabWriter and the router's Handle are the same in the current revision. Each was built once and run in ten interleaved rounds of the package's Slab* and RealisticPipeline benchmarks and the benchmarks module's Logf_TwoScalars, Logf_Router_NoFields and Logf_RouterSlab_NoFields, compared with benchstat. The machine was loaded (load average about 12 on 15 cores), so the spread is wide. B/op and allocs/op are unchanged. One row is marked significant, SlabBufferFragmentationWorstCase at +4.5% (p=0.045, ±29%); every other SlabWriter row, which takes the same check, shows no change, so we read it as noise. The router rows show no change.

@ssgreg ssgreg changed the title fix: panic on use after close and close shared router writers once fix: panic on use of a closed SlabWriter or router Dec 2, 2026
@ssgreg
ssgreg changed the base branch from main to fix/router-close-once December 2, 2026 13:11
@ssgreg

ssgreg commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Force-pushed af0e66d -> 6fe4158 and split. Closing a writer shared by several outputs once, and keeping the close result, moved to #17; this PR now only adds the panics on use after close and is based on #17, since without it the second output's Flush would panic on a shared SlabWriter. The code of both PRs together is the same as before.

@ssgreg

ssgreg commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto the reworked #17. The shared SlabWriter test now puts OutputCloser first and Output second, so it checks that the new Flush panic does not fire during close in that order. The Build doc keeps only the note about stopping logging before close. The description is updated; the benchmark numbers are from the previous revision, whose SlabWriter and Handle code is unchanged.

Base automatically changed from fix/router-close-once to main December 4, 2026 06:40
Writing to a SlabWriter after Close hung once its slab pool ran dry, with the writer's mutex held, so every goroutine that logged after it hung too; with DropOnFull the entries were dropped without a trace. A router kept accepting entries after its close function ran. SlabWriter.Write and Flush and the router's Handle now panic after close, so a shutdown ordering bug shows up as a stack trace pointing at the late writer.
@ssgreg

ssgreg commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #17 was merged. The code is the same as in the previous push; only the parent commit changed.

@ssgreg ssgreg changed the title fix: panic on use of a closed SlabWriter or router fix: panic instead of hanging when a closed SlabWriter or router is used Dec 4, 2026
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