Skip to content

fix: prevent data race between sendToWriteCh and DB close (#2249) - #2324

Open
adityeah8969 wants to merge 1 commit into
dgraph-io:mainfrom
adityeah8969:fix/data-race-on-db-close
Open

fix: prevent data race between sendToWriteCh and DB close (#2249)#2324
adityeah8969 wants to merge 1 commit into
dgraph-io:mainfrom
adityeah8969:fix/data-race-on-db-close

Conversation

@adityeah8969

Copy link
Copy Markdown

Description

Fixes: #2249

Fix a data race between sendToWriteCh and db.Close(). A sender goroutine could pass the blockWrites check in sendToWriteCh while close() simultaneously closed writeCh, causing a "send on closed channel" panic.

Three changes:

  • sendToWriteCh now uses a select over both writeCh <- req and HasBeenClosed() so senders bail cleanly with ErrBlockedWrites during shutdown, instead of sending on a channel that may have been closed.
  • An atomic "writeChInFlight" counter tracks goroutines inside sendToWriteCh so close() knows when no sender remains.
  • A new drainWriteCh() method drains and rejects any orphaned requests from the channel buffer after doWrites exits, ensuring no caller hangs on req.Wait().

Checklist

  • Code compiles correctly and linting passes locally
  • Tests added for new functionality, or regression tests for bug fixes added as applicable

@adityeah8969
adityeah8969 requested a review from a team as a code owner July 22, 2026 00:12
@adityeah8969
adityeah8969 force-pushed the fix/data-race-on-db-close branch from ea2ce1a to bfc9f24 Compare August 12, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Data race on close

1 participant