News for hackers. Stuff that matters.
Re:How to stop Linux threads cleanly by rostayob on Monday October 20, 2025 @12:38PM (#45645875)
This is noted in the blog post, but the problem is that sometimes you don't have the freedom to do so. See this sidenote and the section next to it: https://mazzo.li/posts/stopping-linux-threads.html#fn3 .
Re:How to stop Linux threads cleanly by quietbritishjim on Monday October 20, 2025 @02:49PM (#45647636)
I'll admit: I didn't see that.

But I also disagree with it. Yes, the logical conclusion of starting down that path is that you end up with full on use of coroutines and some IO framework (though I don't see the problem with that). But a simple wrapper for individual calls that is recv+cancel rather than just recv etc is better than any solution mentioned in the blog post.

The fact is, if you want to wait for more than one thing at once at the syscall level (in this case, IO + inter thread cancellation), then the way to do that is to use select or poll or something else actually designed for that.

Re:How to stop Linux threads cleanly by cyberax on Monday October 20, 2025 @03:09PM (#45647883)
I had this problem, and I solved it by farming the known-blocking syscalls to a separate thread pool. Then the calling thread can just abandon the wait. To make it a bit better, you can also use bounded timeouts (~1-2 seconds) with retries for some calls like recvfrom() via SO_TIMEOUT so that the termination time becomes bounded.

This is probably the cleanest solution that is portable.

Re:How to stop Linux threads cleanly by naasking on Tuesday October 21, 2025 @12:45PM (#45657988)
Then those thread pool threads have to be careful not to take locks and other scarce resources that aren't cleaned up then, no?
Re:How to stop Linux threads cleanly by cyberax on Tuesday October 21, 2025 @04:07PM (#45661006)
Sure. But if you only use it to run blocking requests, it's probably going to be pretty trivial. It won't be super-high performance because of all the cache bounces, but if you're not using something like epoll, then you probably are not really focused on performance anyway.