AlexStocks opened a new pull request, #3801: URL: https://github.com/apache/dubbo-go/pull/3801
## What this fixes Fixes #3800. `Shutdown()` closes `logChan` while request goroutines may still be inside `Invoke` -> `logIntoChannel()`. Sending on a closed channel panics, and the `select` with `default` does **not** help: a send on a closed channel is considered ready, so `default` is never selected. The `recover()` in `processLogs` only covers the background goroutine, not the request goroutine. ## Root cause / reproduction The new `TestLogIntoChannelAfterShutdownDoesNotPanic` reproduces this deterministically. Against unmodified `develop` (`d04d7751`) it fails with: ``` Panic value: send on closed channel ``` and the concurrent stress test reports 32/32 goroutines panicking. ## Approach Guard the send/close boundary with an `RWMutex` and a closed flag: - `logIntoChannel` takes `RLock`, returns early when `logClosed`, otherwise runs the existing non-blocking `select`. - `shutdown` takes `Lock` before setting `logClosed` and calling `close(logChan)`. Acquiring the write lock waits for all in-flight sends; since the send `select` has a `default`, the critical section stays short and non-blocking. **Why `close(logChan)` is kept (i.e. why not direction 1 in #3800):** #3564 (merged as `d04d7751`) removed the `default` branch from `drainLogs` and now relies on `ok == false` to finish draining buffered logs. Not closing the channel would make `drainLogs` wait for its full 5s timeout on every shutdown. This PR therefore leaves the channel-close / drain semantics untouched and only serialises sends against the close. Trade-off: one `RLock`/`RUnlock` per access log invocation. The send remains non-blocking, so shutdown cannot stall requests. ## Tests New `filter/accesslog/shutdown_race_test.go`: - `TestLogIntoChannelAfterShutdownDoesNotPanic` — post-shutdown send must not panic. - `TestShutdownKeepsLogsQueuedBeforeClose` — logs queued before shutdown stay drainable, and the channel is closed so `drainLogs` finishes. - `TestInvokeConcurrentWithShutdownDoesNotPanic` — 32 goroutines x 200 `Invoke` calls racing with `shutdown`; panics are recovered per goroutine so an unfixed implementation fails the assertion instead of crashing the test binary. Tests construct a `Filter` directly (without `processLogs` / global state), so they are deterministic and leave no background goroutines behind. Verification (Go 1.25.0, darwin/amd64): - `gofmt -l` clean - `go vet ./filter/accesslog/` clean - `go test ./filter/accesslog/ -count=1` pass - `go test -race ./filter/accesslog/ -count=1` pass - Control run with `filter.go` reverted to unmodified `develop`: 2 of the 3 new tests fail (`send on closed channel`, 32 panics); restoring the fix makes them pass. ## Out of scope `shutdown()` still closes cached file handles while `writeLogToFile` may be running in the drain goroutine. That path produces a logged write error rather than a panic, but late logs can be dropped. Left untouched to keep this fix focused on the panic. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
