-
Notifications
You must be signed in to change notification settings - Fork 471
fix: bound response writes so a stalled client can't wedge a thread forever #2574
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
Open
dunglas
wants to merge
1
commit into
main
Choose a base branch
from
fix-response-write-timeout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
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
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,58 @@ | ||
| package frankenphp_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/dunglas/frankenphp" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestResponseWriteTimeout proves that WithResponseWriteTimeout bounds a | ||
| // stalled reader: the client never reads the response, so the server's socket | ||
| // send buffer fills up and the underlying Write() blocks. Without the option | ||
| // the PHP thread would block in ub_write forever, since | ||
| // frankenphp_force_kill_thread cannot interrupt a write parked in Go's | ||
| // non-blocking netpoller (see php/frankenphp#2553, php/frankenphp#2573); with | ||
| // it, the write is cut off and the handler returns. | ||
| func TestResponseWriteTimeout(t *testing.T) { | ||
| require.NoError(t, frankenphp.Init()) | ||
| defer frankenphp.Shutdown() | ||
|
|
||
| cwd, _ := os.Getwd() | ||
| done := make(chan struct{}) | ||
| handler := func(w http.ResponseWriter, r *http.Request) { | ||
| defer close(done) | ||
| req, err := frankenphp.NewRequestWithContext(r, | ||
| frankenphp.WithRequestDocumentRoot(cwd+"/testdata/", false), | ||
| frankenphp.WithResponseWriteTimeout(300*time.Millisecond), | ||
| ) | ||
| require.NoError(t, err) | ||
| require.NoError(t, frankenphp.ServeHTTP(w, req)) | ||
| } | ||
|
|
||
| ts := newRawServer(t, handler) | ||
| defer ts.Close() | ||
|
|
||
| conn, err := net.Dial("tcp", ts.Addr()) | ||
| require.NoError(t, err) | ||
| defer func() { _ = conn.Close() }() | ||
|
|
||
| // Shrink the client's receive window so the server's send buffer fills | ||
| // after only a handful of writes instead of relying on OS defaults. | ||
| require.NoError(t, conn.(*net.TCPConn).SetReadBuffer(1024)) | ||
|
|
||
| _, err = fmt.Fprintf(conn, "GET /slow-write.php HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", ts.Addr()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Never read the response: that's the stalled reader. | ||
| select { | ||
| case <-done: | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("the write timeout did not release the PHP thread") | ||
| } | ||
| } |
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,11 @@ | ||
| <?php | ||
|
|
||
| // Write continuously so a stalled reader eventually blocks the underlying | ||
| // socket write. Capped at 10s so an unpatched build fails the test instead of | ||
| // hanging it forever. | ||
| ob_implicit_flush(true); | ||
| $start = microtime(true); | ||
| while (microtime(true) - $start < 10) { | ||
| echo str_repeat('x', 65536); | ||
| } | ||
| echo 'did not time out'; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SetWriteDeadlinebounds the wholeWritecall, and withoutput_buffering=0php_output_op hands ub_write an entire echo in a single call, so this is a "transfer this buffer within 60s" cap rather than an idle timeout. A 20 MB response to a sub-340 KB/s client is truncated viaphp_handle_aborted_connection(). nginx'ssend_timeoutis explicitly "set only between two successive write operations, not for the transmission of the whole response", and Caddy shipswrite_timeoutunset for this reason.So, I guess you should slice the write and re-arm per chunk, or drop the idle-timeout/send_timeout framing from module.go, docs/config.md and docs/security.md.