Skip to content

http: close a connection after its response times out - #5133

Open
KernelClint wants to merge 1 commit into
secdev:masterfrom
KernelClint:http-close-after-timeout
Open

http: close a connection after its response times out#5133
KernelClint wants to merge 1 commit into
secdev:masterfrom
KernelClint:http-close-after-timeout

Conversation

@KernelClint

Copy link
Copy Markdown
Contributor

HTTP_Client.request() sends a request, waits for the response, and keeps the connection open so a
later request to the same host and port can reuse it.

When the wait times out, sr1() returns nothing and
scapy/layers/http.py:938-948
breaks out of the request loop without touching the socket. The connection stays in the reusable
pool with an unanswered request outstanding on it.

If the response then arrives late, it is sitting on the stream when the next request goes out.
Matching does not help: at scapy/layers/http.py:591-592 any HTTP response answers any HTTP
request, so the stale one is returned as the new request's answer. A run that asked for /second
received the body first.

The change discards the connection instead:

             if not resp:
+                self.close()
+                self._sockinfo = None
                 break

An HTTP/1.1 client has no way to tell whether a later response on that stream belongs to the
abandoned request, so reuse is not safe. Requests that get a timely response are unaffected — only
the timeout path changes.

The added regression drives a client whose first response never arrives and asserts the second
request opens a new connection and gets its own answer. Without the source change it fails.

Performance was measured on one computer, before and after the fix: a normal request took 32.5 µs
before and 31.7 µs after. Repeat runs moved by about 4%, so that difference is smaller than the
test can distinguish.

@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.64%. Comparing base (b3bbcc8) to head (5577ad7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5133   +/-   ##
=======================================
  Coverage   80.63%   80.64%           
=======================================
  Files         390      390           
  Lines       96936    96938    +2     
=======================================
+ Hits        78168    78176    +8     
+ Misses      18768    18762    -6     
Files with missing lines Coverage Δ
scapy/layers/http.py 83.75% <100.00%> (+0.36%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant