Skip to content

fix: harden FBHTTPServer/FBTCPSocket against races and protocol gaps - #1224

Open
mykola-mokhnach wants to merge 1 commit into
masterfrom
bugs
Open

fix: harden FBHTTPServer/FBTCPSocket against races and protocol gaps#1224
mykola-mokhnach wants to merge 1 commit into
masterfrom
bugs

Conversation

@mykola-mokhnach

Copy link
Copy Markdown

Follow-up fixes for the Network.framework HTTP server (#1221) found while porting it to a downstream WebDriverAgent fork:

  • Defer FBWebServer's shutdown delegate call so the /wda/shutdown response is actually written before the socket/connections are torn down.
  • Cancel FBTCPSocket connections on the socket queue during -stop so an already-issued write isn't raced by the cancellation.
  • Cancel the listener on startup timeout/failure instead of just dropping the reference, and surface POSIX errors (e.g. EADDRINUSE) undecorated so FBWebServer's port-retry check can match them.
  • Stop accumulating received data via repeated mutableCopy+append (O(n^2)); append into a single NSMutableData instead.
  • Cache parsed request headers per connection while the body streams in, instead of re-scanning/re-parsing the whole buffered header block on every incoming chunk.
  • Remove a client's buffered state before responding with a terminal error (413/400/501) so a still-streaming body can't re-trigger the same response.
  • Reject Transfer-Encoding: chunked explicitly (501) instead of silently treating the request as bodyless and desyncing the connection.
  • Apply default headers (CORS, Server) to error responses, not just successfully-routed ones.
  • Give multiple "*" wildcard segments in one route path distinct params keys instead of overwriting a single "wildcards" key.
  • Return valid WebDriver protocol error JSON for 400/404/413/501 responses instead of raw strings, matching the shape route handlers already use.

@eglitise

Copy link
Copy Markdown

Looks like there are some merge conflicts

Follow-up fixes for the Network.framework HTTP server (#1221) found while
porting it to a downstream WebDriverAgent fork:
- Defer FBWebServer's shutdown delegate call so the /wda/shutdown response
  is actually written before the socket/connections are torn down.
- Cancel FBTCPSocket connections on the socket queue during -stop so an
  already-issued write isn't raced by the cancellation.
- Cancel the listener on startup timeout/failure instead of just dropping the
  reference, and surface POSIX errors (e.g. EADDRINUSE) undecorated so
  FBWebServer's port-retry check can match them.
- Stop accumulating received data via repeated mutableCopy+append (O(n^2));
  append into a single NSMutableData instead.
- Cache parsed request headers per connection while the body streams in,
  instead of re-scanning/re-parsing the whole buffered header block on every
  incoming chunk.
- Remove a client's buffered state before responding with a terminal error
  (413/400/501) so a still-streaming body can't re-trigger the same response.
- Reject Transfer-Encoding: chunked explicitly (501) instead of silently
  treating the request as bodyless and desyncing the connection.
- Apply default headers (CORS, Server) to error responses, not just
  successfully-routed ones.
- Give multiple "*" wildcard segments in one route path distinct params keys
  instead of overwriting a single "wildcards" key.
- Return valid WebDriver protocol error JSON for 400/404/413/501 responses
  instead of raw strings, matching the shape route handlers already use.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@mykola-mokhnach

Copy link
Copy Markdown
Author

Looks like there are some merge conflicts

conflict resolved

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