Skip to content

net: stop using a socket once a wait reports an error - #36953

Closed
darosior wants to merge 1 commit into
bitcoin:masterfrom
darosior:2611_disconnect_oob
Closed

darosior wants to merge 1 commit into
bitcoin:masterfrom
darosior:2611_disconnect_oob

Conversation

@darosior

@darosior darosior commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

ConnMan and the HTTP server would still try to read (and send) when a wait reports ErrorEvent. This means they could still read even when a read was not requested.

On select() based platform (BSDs, macOS and Windows), ErrorEvent can be returned if a peer sends us urgent data (we register the exception set because of a Windows quirk). However we can't read that urgent data, and this means we could be continuously woken up.

There is a number of things we could do to improve the situation here (see discussions in #36761), but in the meantime fix this bug by simply disconnecting immediately upon error. For consistency, adopt the same behaviour across all callers.

This PR is an alternative to #36716.

ConnMan and the HTTP server would still try to read (and send) when a
wait reports ErrorEvent. This means they could still read even when a
read was not requested.

On select() based platform (BSDs, macOS and Windows), ErrorEvent can be
returned if a peer sends us urgent data (because of a Windows quirk).
However we can't read that urgent data, and this means we could be
continuously woken up.

There is a number of things we could do to improve the situation here
(see discussions in bitcoin#36761), but in the meantime fix this bug by simply
disconnecting immediately upon error. For consistency, adopt the same
behaviour across all callers.
@DrahtBot DrahtBot added the P2P label Nov 23, 2026
@DrahtBot

DrahtBot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Comment thread src/httpserver.cpp
bool recv_ready = events.occurred & Sock::RecvEvent;
bool err_ready = events.occurred & Sock::ErrorEvent;

if (err_ready) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This prevents us from reading any more data after an ERR/HUP event, which is counter to how the typical TCP flow works. A half-close is a perfectly reasonable thing for a peer to do when they're done sending.

I'm not opposed to this as it's clearly a reasonable defensive measure to take, but I'm nervous that the current p2p network relies on this behavior to some extent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this is incorrect. A half close sends a FIN, and poll() returns POLLIN for this. So this commit does not change behaviour in this case.

However, a peer abruptly close()ing its connection right after sending us data may result in a RST, for which poll() returns ERR/HUP and for which we would now disconnect early. But that's hardly a "perfectly reasonable thing to do".

I'm running some probes now to get an idea of how well we process messages in practice in each of these situations, before and after the PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not at all standard across poll implementations. In my tests I've seen all combinations of IN/ERR/HUP. It may work currently for Linux (which, I understand, is our only poll code-path for now), but we can't just look at one impl and assume that's how poll works. For example, additionally subscribing to POLLRDHUP on Linux brings us closer to other impls, as it causes us to skip the read queue for more HUP events. And what's the point of knowingly opting-out of those events that other OSs would receive?

We need to step back and decide what outcome we want, then back into the impl for each platform.

Our current code does its best to read from sockets after HUP/ERROR events until the socket has been drained. That makes our lives much more difficult. If we decide to drop that requirement, it shapes much of our other decision making. But I would be much more comfortable making that change alone, and only after some real-world testing, actually committing to that behavior.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another example of where this differs is with our p2p v2 re-connection code. It assumes that if we manage to write a few bytes to our peer before we're disconnected, we should retry with v1. But if our peer disconnects us before we attempt to write those bytes (this happens on OSX when using poll and disconnecting on HUP/ERR), we don't bother trying to reconnect. This is in fact the correct behavior, but we don't detect it on Linux because we don't subscribe to POLLRDHUP and therefore the write is allowed before the disconnect. As a result, the testing around all this is wonky.

So again, I suggest that we decide on the the behaviors that we want, add extensive tests for them, and then start changing the impls to get those tests to pass. Otherwise, we're going to start pulling levers and twisting knobs and end up in a situation where no two platforms do the same thing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I absolutely agree about not just pulling levers and twisting knobs, which has been why i have opened #36761 to discuss what we want to do medium/long term, and i pushed back against #36716 which appeared to me as going further down the inconsistent knob-twisting direction.

But perhaps we should take a step back and consider what we want to achieve here. I agree with everything you said here, but was assuming this would be a major refactoring that would be backportable. From this premise, i suggested the approach of this PR which i think is the most reasonable minimal patch for an issue we've considered to be important so far and our users are exposed in the wild.

But maybe that's the wrong premise. I certainly do not want to risk breaking some p2p compatibility to fix a CPU spin on second tier platforms. So maybe we should just focus on cleaning up our socket implementation for the next major release, instead of backporting some parts of that work to fix the issue on older versions, which will necessarily boil down to more pin pointed know twisting if we don't want to backport the full refactor (which seems unreasonable).

Will come back with some results for my measurements soon.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It may work currently for Linux (which, I understand, is our only poll code-path for now), but we can't just look at one impl and assume that's how poll works. For example, additionally subscribing to POLLRDHUP on Linux brings us closer to other impls, as it causes us to skip the read queue for more HUP events.

Sorry, that does not make sense to me. We are not looking to remodel our entire socket implementation in this PR, but to fix a bug in a backportable manner, without introducing regressions. Testing for regressions on the one platform we support appears to me like an appropriate way to check this.

And what do you mean just looking at one impl? I'm looking at the implementation of the Linux kernel, which is what's relevant for us. We are already relying on their behaviour being consistent, and given their concerns about not breaking user space, it seems extremely unlikely that they would suddenly change how poll() works.

And how is adding new subscription relevant for this bug fix? I understand we may want to overhaul our socket implementation to make it consistent, documented, understandable and document its invariants, and i'm on board with that, but it seems to me orthogonal to what we are trying to achieve in this PR. It's akin to if i was objecting to Sjors' alternative by saying "yeah but what if we had the code this way instead?" -- well yeah it would break, the code is written for the current logic, not another one that doesn't currently exist.

Now i agree we want to take all this into account for the big refactor of switching all our platforms to use poll() and be consistent with each others. But it seems we are talking past each other here, where i'm trying to fix the bug on older versions, and you object because it's not the best first step for a complete overhaul.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, sorry, those responses got rambly.

I was reacting to your descriptions of poll behavior above, and noting that it's a description of how poll on LInux works. Yes that's fine for now, but I didn't want other reviewers to confuse it for a description of how poll works generally, so I provided some counter-examples. For the sake of this PR, those aren't relevant.

@darosior
darosior marked this pull request as draft November 23, 2026 19:33
@darosior

Copy link
Copy Markdown
Member Author

Converting to draft as i would prefer doing #36716 (if it can be trimmed down to only the useful parts) to avoid litigating the importance of draining on reset connections, for a fix we intend to backport.

@darosior darosior closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants