Skip to content

fix leaking connections when user client closes connection #222

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

Merged
merged 5 commits into from
Sep 15, 2023

Conversation

koonpeng
Copy link
Contributor

Same as #220, but ported to this repo.


When the user client closes the connection, it is not propagated to the backend. For simple "one shot" requests, there is no huge impact as the response would just be dropped, however for long running requests (chunked encoding or websockets), the backend doesn't know that the user client has closed the connection and it keeps sending new updates to the relay client, which also doesn't know and so keep forwarding that to the relay server.

The fix applied is:

  • Add StopRelayRequest to the broker to "forget" a relaying request. This will cause the relay server to response with a permanent error the next time the relay client tries to send a response for that id. This will in turn cause the relay client to close the connection to the backend.
  • There is a bug in the relay client when checking for backoff permanent errors. When the backoff operation encounters a permanent error, it actually unwraps it and return the underlying error, but the client is still checking for backoff.Permanent, resulting in the check to always fail and never handling it.

// A permanent error suggests the request should be aborted.
log.Printf("[%s] %s", *resp.Id, err)
log.Printf("[%s] Closing backend connection", *resp.Id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only log a single line:
log.Printf("[%s] Relay error: %v, closing backend connection", *resp.Id, err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in 301b04d

@drigz
Copy link
Contributor

drigz commented Sep 13, 2023

Thanks for moving the PR, the CI looks happy now. Will you also address the other comments from #220?

Signed-off-by: Teo Koon Peng <[email protected]>
Copy link
Contributor

@ensonic ensonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@drigz drigz merged commit 26a1153 into main Sep 15, 2023
@drigz drigz deleted the kp/fix-unclosed-connections branch September 15, 2023 08:18
@drigz drigz mentioned this pull request Sep 15, 2023
drigz added a commit that referenced this pull request Sep 15, 2023
drigz added a commit that referenced this pull request Sep 15, 2023
It causes the postsubmit integration test to fail with:

```
error: Timeout occurred
TEST FAILED: echo command did not run, output was "", want "hello"
```

when testing `kubectl exec`.


https://source.cloud.google.com/results/invocations/8f0b86f7-c5c4-41a3-9e4c-43e325d9e1ae/targets/%2F%2Fsrc%2Fgo%2Ftests:relay_test/log

- Revert "Use %v instead of %s in relay logs (#224)"
- Revert "fix leaking connections when user client closes connection
(#222)"
koonpeng added a commit that referenced this pull request Sep 18, 2023
This reverts commit 1b29414.
drigz pushed a commit that referenced this pull request Sep 18, 2023
Re-reverting #222 with a fix to relay_test.sh from the postsubmit test.

The problem is mistakenly using `w.Write` at server.go:260, the response
writer is no longer valid after the connection has been hijacked, we
need to use `bufrw.Write` instead.

---------

Signed-off-by: Teo Koon Peng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants