Skip to content

core: guard against RST packets during read - #76

Closed
Bogdanp wants to merge 3 commits into
racket:masterfrom
Bogdanp:fix-tcp-read-exns
Closed

Bogdanp wants to merge 3 commits into
racket:masterfrom
Bogdanp:fix-tcp-read-exns

Conversation

@Bogdanp

@Bogdanp Bogdanp commented Oct 14, 2019

Copy link
Copy Markdown
Contributor

This prevents errors such as

tcp-read: error reading
  system error: Connection reset by peer; errno=54
  errortrace...:
  context...:
   /Applications/Racket v7.4/collects/racket/port.rkt:1160:4: try-again

from appearing in application output after clients terminate their connections abruptly (without sending a FIN packet).

This code can be used to exercise the problem:

#lang racket

(require racket/tcp)

(for ([_ (in-range 100)])
  (define-values (in out)
    (tcp-connect "127.0.0.1" 8000))

  (parameterize ([current-output-port out])
    (displayln "GET / HTTP/1.1")
    (displayln "")
    (flush-output))

  (read-string 100 in)

  (tcp-abandon-port in)
  (tcp-abandon-port out))

This prevents errors such as

    tcp-read: error reading
      system error: Connection reset by peer; errno=54
      errortrace...:
      context...:
       /Applications/Racket v7.4/collects/racket/port.rkt:1160:4: try-again

from appearing in application output after clients terminate their
connections abruptly (without sending a FIN packet).

This code can be used to exercise the problem:

    #lang racket

    (require racket/tcp)

    (for ([_ (in-range 100)])
      (define-values (in out)
        (tcp-connect "127.0.0.1" 8000))

      (parameterize ([current-output-port out])
        (displayln "GET / HTTP/1.1")
        (displayln "")
        (flush-output))

      (read-string 100 in)

      (tcp-abandon-port in)
      (tcp-abandon-port out))
@Bogdanp

Bogdanp commented Oct 14, 2019

Copy link
Copy Markdown
Contributor Author

For reference, the thread in which the exception gets raised is here: https://github.com/racket/racket/blob/ca2eafbfc404207b4f8d5b1423dc1f7bb48afcef/racket/collects/racket/port.rkt#L1142-L1146 . Perhaps that code could also be updated to handle this case a bit better?

cc @mflatt

It doesn't appear that the way the loop was previously set up was
buying us anything over this simpler version.
@Bogdanp

Bogdanp commented Oct 14, 2019

Copy link
Copy Markdown
Contributor Author

I realized I could simplify the whole connection loop by ditching all of the evt stuff. It looks like all the tests still pass and the original issue is still fixed. Was there a reason the loop was written that way (support for an older version of Racket?) or was it just entropy doing its thing?

The client socket might get closed while we're blocking on the peek so
this change ensures that we handle that particular case gracefully.
@mflatt

mflatt commented Oct 14, 2019

Copy link
Copy Markdown
Member

I think there's confusion here about port-closed-evt. It's not about the other end of a connection-based port. It's about the port itself being closed concurrently in the same process with close-input-port or close-output-port. I imagine that the web server is set up so that the server may decide to terminate a connection by closing a port, but I don't really know.

Adding a peek-bytes-avail! before the sync causes peek-bytes-avail! to raise an exception if the port is closed with close-input-port, instead of letting the event created by port-closed-evt handle the close. So, assuming that the handling of a closed port (as opposed to a closed connection) is relevant, adding peek-bytes-avail! would not be the right thing.

Meanwhile, there really should be no difference to using peek-bytes-avail! and sync with peek-bytes-evt as far as the potential for ECONNRESET errors. For a given scenario, there is very likely a timing difference that can make one or the other trigger ECONNRESET, but neither one would consistently avoid ECONNRESET (unless I've missed something about how you're using them here).

@Bogdanp

Bogdanp commented Oct 14, 2019 •

Copy link
Copy Markdown
Contributor Author

I think there's confusion here about port-closed-evt. It's not about the other end of a connection-based port. It's about the port itself being closed concurrently in the same process with close-input-port or close-output-port. I imagine that the web server is set up so that the server may decide to terminate a connection by closing a port, but I don't really know.

That's a good point I hadn't considered. I'll take another look at this tomorrow with that in mind just to be sure I didn't miss anything with the latter changes. Thanks!

Meanwhile, there really should be no difference to using peek-bytes-avail! and sync with peek-bytes-evt as far as the potential for ECONNRESET errors. For a given scenario, there is very likely a timing difference that can make one or the other trigger ECONNRESET, but neither one would consistently avoid ECONNRESET (unless I've missed something about how you're using them here).

You're right that they are both susceptible to running into resets. The difference is that peek-bytes-evt swallows the exception because the exception gets raised in the thread it sets up to wait for bytes to become available (this code which runs this code). From the perspective of code that uses peek-bytes-evt, the event never becomes ready for synchronization and from the perspective of someone running the code, an error gets printed to standard out. Sorry I wasn't clear from the beginning!

@mflatt

mflatt commented Oct 14, 2019

Copy link
Copy Markdown
Member

Ah, I see. That does sound like a bug in peek-bytes-evt.

mflatt added a commit to racket/racket that referenced this pull request Oct 14, 2019
Propoagate read errors to the `sync` that is applied to an event,
instead of writing the events to stdout.

Related to racket/web-server#76
@Bogdanp Bogdanp closed this Oct 16, 2019
@Bogdanp
Bogdanp deleted the fix-tcp-read-exns branch October 16, 2019 10:05
MarcKaufmann pushed a commit to MarcKaufmann/racket that referenced this pull request Apr 15, 2020
Propoagate read errors to the `sync` that is applied to an event,
instead of writing the events to stdout.

Related to racket/web-server#76
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.

2 participants