Add test for #4790: Race condition between StreamRow() and Close()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Nathan Wallace
2025-11-11 22:29:34 +08:00
parent e02d7d7d0d
commit 780b2e65b4

View File

@@ -66,6 +66,43 @@ func TestResult_ConcurrentReadAndClose(t *testing.T) {
}
}
func TestResult_ConcurrentStreamRowAndClose(t *testing.T) {
// Demonstrates bug #4790 - Race condition between StreamRow() and Close()
// When StreamRow() and Close() are called concurrently, we can get a
// "send on closed channel" panic if Close() closes the RowChan while
// StreamRow() is trying to send to it.
cols := []*queryresult.ColumnDef{
{Name: "id", DataType: "integer"},
{Name: "name", DataType: "text"},
}
// Run this test multiple times to increase likelihood of triggering the race
for i := 0; i < 100; i++ {
result := NewResult(cols)
// Start a goroutine that sends rows
go func() {
for j := 0; j < 10; j++ {
result.StreamRow([]interface{}{j, "test"})
}
}()
// Start a goroutine that consumes rows
go func() {
for range result.RowChan {
// Just drain the channel
}
}()
// Close immediately, creating a race with StreamRow
result.Close()
}
// If we get here without panicking, the test passes
// Run with -race flag to detect the race condition
}
func TestWrapResult_NilResult(t *testing.T) {
// WrapResult should handle nil input gracefully
result := WrapResult(nil)