-
Notifications
You must be signed in to change notification settings - Fork 149
fix: prevent indefinite hangs during TLS operations #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
robutrader
wants to merge
2
commits into
projectdiscovery:main
from
robutrader:fix/indefinite-hang-timeout
+231
−14
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package output | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFileWriterPeriodicFlush(t *testing.T) { | ||
| // Create temp file | ||
| tmpDir := t.TempDir() | ||
| tmpFile := filepath.Join(tmpDir, "test_output.jsonl") | ||
|
|
||
| // Create writer | ||
| writer, err := newFileOutputWriter(tmpFile) | ||
| require.NoError(t, err) | ||
| defer writer.Close() | ||
|
|
||
| // Write exactly flushThreshold entries | ||
| for i := 0; i < flushThreshold; i++ { | ||
| err := writer.Write([]byte(`{"test": "data"}`)) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // After flushThreshold writes, data should be flushed to disk | ||
| // Read file contents without closing writer | ||
| contents, err := os.ReadFile(tmpFile) | ||
| require.NoError(t, err) | ||
|
|
||
| // File should have content (periodic flush worked) | ||
| assert.NotEmpty(t, contents, "file should have content after %d writes due to periodic flush", flushThreshold) | ||
| } | ||
|
|
||
| func TestFileWriterWriteCount(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| tmpFile := filepath.Join(tmpDir, "test_count.jsonl") | ||
|
|
||
| writer, err := newFileOutputWriter(tmpFile) | ||
| require.NoError(t, err) | ||
| defer writer.Close() | ||
|
|
||
| // Write some entries | ||
| for i := 0; i < 50; i++ { | ||
| err := writer.Write([]byte(`{"count": 1}`)) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // Verify write count is tracked | ||
| count := writer.writeCount.Load() | ||
| assert.Equal(t, int64(50), count, "write count should be 50") | ||
| } | ||
|
|
||
| func TestFileWriterCloseFlushes(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| tmpFile := filepath.Join(tmpDir, "test_close.jsonl") | ||
|
|
||
| writer, err := newFileOutputWriter(tmpFile) | ||
| require.NoError(t, err) | ||
|
|
||
| // Write less than flushThreshold (so periodic flush won't trigger) | ||
| for i := 0; i < flushThreshold/2; i++ { | ||
| err := writer.Write([]byte(`{"close": "test"}`)) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // Close should flush remaining data | ||
| err = writer.Close() | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify all data was written | ||
| contents, err := os.ReadFile(tmpFile) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, contents, "file should have content after close") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package ztls | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestHandshakeTimeoutCancellation verifies that the handshake timeout | ||
| // properly cancels when the context is cancelled, rather than blocking | ||
| // indefinitely on the handshake operation. | ||
| func TestHandshakeTimeoutCancellation(t *testing.T) { | ||
| // Create a very short timeout context | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| // Verify context cancellation works as expected | ||
| select { | ||
| case <-ctx.Done(): | ||
| // Expected - context should timeout | ||
| assert.Error(t, ctx.Err(), "context should have error after timeout") | ||
| case <-time.After(100 * time.Millisecond): | ||
| t.Fatal("context timeout did not trigger") | ||
| } | ||
| } | ||
|
|
||
| // TestContextSelectBehavior verifies that a goroutine-based approach | ||
| // allows the select statement to properly choose between completion | ||
| // and context cancellation. | ||
| func TestContextSelectBehavior(t *testing.T) { | ||
| // This test demonstrates the correct pattern for timeout-based | ||
| // handshakes: running the blocking operation in a goroutine | ||
| // so the select can properly evaluate both cases. | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| resultChan := make(chan string, 1) | ||
|
|
||
| // Simulate a slow operation in a goroutine | ||
| go func() { | ||
| time.Sleep(200 * time.Millisecond) // Slower than timeout | ||
| resultChan <- "completed" | ||
| }() | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| // This is the expected path - timeout should win | ||
| assert.Equal(t, context.DeadlineExceeded, ctx.Err()) | ||
| case result := <-resultChan: | ||
| t.Fatalf("should have timed out, but got result: %s", result) | ||
| } | ||
| } | ||
|
|
||
| // TestNoDeadlockOnTimeout ensures that the timeout mechanism doesn't | ||
| // cause goroutine leaks or deadlocks. | ||
| func TestNoDeadlockOnTimeout(t *testing.T) { | ||
| done := make(chan struct{}) | ||
|
|
||
| go func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| errChan := make(chan error, 1) | ||
|
|
||
| // Run blocking operation in goroutine | ||
| go func() { | ||
| time.Sleep(100 * time.Millisecond) // Simulate slow handshake | ||
| errChan <- nil | ||
| }() | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| // Timeout occurred - this is expected | ||
| case <-errChan: | ||
| // Operation completed | ||
| } | ||
|
|
||
| close(done) | ||
| }() | ||
|
|
||
| // Test should complete without deadlock | ||
| select { | ||
| case <-done: | ||
| // Success - no deadlock | ||
| case <-time.After(500 * time.Millisecond): | ||
| t.Fatal("test timed out - possible deadlock") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.