You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
should concurrent IPC requests directed to the same thread cause a crash? #33923
recently I reported #33554, which was fixed by the introduction of interruptWait via #33575
then I noticed crashes for similar reasons (although unrelated to waitNext), which I explain in detail here: stratum-mining/sv2-apps#88 (comment)
TLDR: I was trying to do a getBlock while a submitSolution was happening at the same time, both against the same Bitcoin Core thread
this made me realize that regardless of whether the call is going to block for a long time (like waitNext), I simply cannot leave room for concurrent calls against the same Bitcoin Core thread
More broadly, I think I'd try just having a c++ "waiter" thread dedicated to running waitNext calls, and a c++ "worker" thread to run all other IPC operations besides waitNext. The "waiter" thread should be mostly blocked monitoring mempool and network, and the "worker" thread should be available to do anything else you might need like fetching and submitting block data.
overall, I'm getting to a place where I'm having to go great lengths in terms of defensive programming just to avoid crashing Bitcoin Core
I'm still evaluating the trade-offs of potential defensive strategies, namely:
having synchronization primitives that avoid overlapping requests against the same Bitcoin Core thread
instantiating multiple Bitcoin Core threads, one dedicated for each potentially concurrent request
but I digress... the main point I wanted to articulate here was: shouldn't Bitcoin Core simply not crash if two requests arrive for the same thread?
ideally, a naive client should simply be forced to wait a bit longer if they send concurrent requests against the same Bitcoin Core thread, instead of being forced to avoid doing this at all cost because it could cause havoc on the other side of the IPC connection
shouldn't Bitcoin Core simply not crash if two requests arrive for the same thread?
Yes this shouldn't crash. Hopefully, if you are using a version of bitcoin after bitcoin-core/libmultiprocess#214 (part of #33518 in master and part of #33519 in 30.x branch) you should just see "thread busy" errors instead of crashes, and it is a bug if there are still crashes.
ideally, a naive client should simply be forced to wait a bit longer if they send concurrent requests against the same Bitcoin Core thread
This is reasonable and would be pretty easy to implement, but it raises the question of how many requests should be allowed? Should it allow 5, 10, or 100 requests before returning "thread busy"? Or an arbitrary amount? Or a configurable amount?
My thinking has been that if a client needs a request queue, it is probably better for the client implement the queue, than to have the server provide one because the client should know its own needs better. Also, not every client needs a queue. For example, Sjor's SV2 client is multithreaded and only makes blocking requests. It just creates one server thread for each client thread, so every server thread is guaranteed to not be busy when a request comes in.
Since the rust client is single threaded and nonblocking, it needs to create different server threads for those requests to execute in parallel like the c++ client does, or it needs to have a queue so requests created in parallel can be executed serially on a single server thread.
In practice, I think it make sense for the rust client to just create 2 server threads: a waiting thread to execute waitNext() calls, and a worker thread using a queue to execute all other calls.
It if would help, I could add a enableRequestQueue() or similar method to the Thread interface so the rust client can ask for the requests sent to a thread to be queued.
However, I think it would probably be straightforward to implement a queue in rust on the client side instead. And I wouldn't want queuing to be enabled on the server by default because it could mask other problems. Like if a request was accidentally sent to the waiting thread rather than the worker thread, it could just sit there forever if queuing was enabled on the waiting thread, instead of returning an error.
Thanks for reporting the problem here and I hope this can lead to a good solution.
I guess the main thing I wanted to report here is a duplicate of bitcoin-core/libmultiprocess#214, so it's good to know these crashes won't happen on v30.x!
how many requests should be allowed? Should it allow 5, 10, or 100 requests before returning "thread busy"? Or an arbitrary amount? Or a configurable amount?
it would be up to the client to know what are the reasonable bounds for backpressure so that it can avoid the need to handle errors, so I'd say that a configurable amount with a reasonably large default
but since you also mentioned:
And I wouldn't want queuing to be enabled on the server by default because it could mask other problems.
I wouldn't necessarily frame this as an actual feature request, mainly because we're working around this without having to resort to server-side queueing.
Since the rust client is single threaded and nonblocking, it needs to create different server threads for those requests to execute in parallel like the c++ client does, or it needs to have a queue so requests created in parallel can be executed serially on a single server thread.
currently we're going with the first approach, so now we create 5 server threads, one dedicated for each potentially concurrent call:
submitSolution
getHeader
getCoinbaseTx
getCoinbaseMerklePath
waitNext
plus some ad-hoc threads that are created ephemerally whenever we need to do a getBlock (either to reply to a RequestTransactionData or because we want to dump a valid solution to disk)
to be honest, we could have gotten away with only 2 by forcing getHeader, getCoinbaseTx, getCoinbaseMerklePath and waitNext to be done sequentially under the same server thread
but we're trying to squeeze out every possible micro/nano/pico second out of the way with concurrent execution of getHeader, getCoinbaseTx, getCoinbaseMerklePath so we can craft a NewTemplate message ASAP (the ones for mempool updates are actually rate-limited, so we're not really in a race... but for chain tip updates we do want this to happen as fast as possible)
with this approach, queuing requests on the server side is not needed at all.
but it made me wonder about something (which I was eventually going to be forced to consider anyways):
we eventually want to adapt this code so that it can become a sidecar that's attached to Bitcoin Core, which then acts as a Sv2 Template Provider, listening on a TCP port and catering for as many clients as possible... so now, for every client that connects to it, we create 5 new threads on Bitcoin Core
whether we create 1, 2 or 5 threads for each client is not too relevant, but my main question is whether there are there protection mechanisms in place in case we try to create too many threads at once? if so, what happens once Bitcoin Core is no longer willing to create new threads?
to be honest, we could have gotten away with only 2 by forcing getHeader, getCoinbaseTx, getCoinbaseMerklePath and waitNext to be done sequentially under the same server thread
I think you should bench this before deciding if it needs optimising and also check whether it's actually faster and not e.g. slowing the node responses down with overhead.
If getHeader, getCoinbase(Tx) and getCoinbaseMerklePath really need to be called in parallel, then I think it would be better if the BlockTemplate interface is somehow pre-populated with those values - avoiding these round trips entirely.
my main question is whether there are there protection mechanisms in place in case we try to create too many threads at once?
There aren't any currently, and making a lot of threads is probably the easiest way an IPC client could DoS the node.
It wouldn't be hard to create a limit though, if there is a use-case.
I do think creating 5 threads for each client that needs to receive templates sounds like overkill, and would suggest trying to implement an approach with less overhead. Ideally the SV2 template provider be able to take a single bitcoin core template and broadcast it out to many clients, instead of each client needing unique templates.
I also agree with sjors it doesn't seem ideal to be calling getters like getHeader, getCoinbaseTx, and getCoinbaseMerklePath in parallel. If we can change the interface to provide a single method that will return all the information needed so you don't need to make multiple calls, that would be better.
My thinking has been that if a client needs a request queue, it is probably better for the client implement the queue, than to have the server provide one because the client should know its own needs better.
Replying to myself, I now am rethinking my previous thinking. One case where a server queue is clearly better than a client queue is when a client wants to request multiple pieces of data and have the data sent back sequentially. Without a server queue, the client needs to wait for each piece of data to be received before requesting the next piece of data, which is not efficient. With a server queue, data can be streamed back much more nicely. So now I am thinking about ways to implement a queue. And I think it might be possible to do without creating an separate data structure or explicit queue limit by simply attaching data to capnproto promises. This would get rid of the "thread busy" error entirely.
I agree with @Sjors that we should just expose those values in one call to avoid the round trip, @plebhash it would also be nice to benchmark what @Sjors suggests.
I also agree on the potential of throttling if we allow an arbitrary number of threads. It should definitely be rate-limited.
to be honest, we could have gotten away with only 2 by forcing getHeader, getCoinbaseTx, getCoinbaseMerklePath and waitNext to be done sequentially under the same server thread
I think you should bench this before deciding if it needs optimising and also check whether it's actually faster and not e.g. slowing the node responses down with overhead.
I agree with @Sjors that we should just expose those values in one call to avoid the round trip, @plebhash it would also be nice to benchmark what @Sjors suggests.
collect 100 samples of fetching template data under two different strategies:
sequentially execute getHeader, then getCoinbaseTx, then getCoinbaseMerklePath, all against the same pre-allocated server thread
concurrently execute getHeader, getCoinbaseTx, and getCoinbaseMerklePath, against 3 different pre-allocated server threads
notes:
node was Bitcoin Core v30.2 running on mainnet
all requests were done against the same BlockTemplate (I wonder if that could skew or bias the results in any meaningful way?)
here's the results:
fetch_template/sequential/single_thread
time: [505.89 µs 507.80 µs 509.73 µs]
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe
fetch_template/concurrent/multiple_threads
time: [367.33 µs 368.71 µs 370.15 µs]
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) high mild
1 (1.00%) high severe
Note: The first commit bitcoin-core/libmultiprocess@8fe9da2 from bitcoin-core/libmultiprocess#240 removes restriction on clients sending many requests at the same time to the same server thread. With that change the server just queues requests and executes them in the order received.
I went ahead and implemented this after realizing the server-side queuing could be implemented by just chaining promises together without needing to add new data structure, and after realizing the client-side queuing I suggested earlier was inherently less efficient because it requires clients to wait to receive responses from previous requests before sending new requests.
There is also some new python code in #34284 testing this behavior.
Activity
ryanofsky commentedon Nov 21, 2025
Yes this shouldn't crash. Hopefully, if you are using a version of bitcoin after bitcoin-core/libmultiprocess#214 (part of #33518 in master and part of #33519 in 30.x branch) you should just see "thread busy" errors instead of crashes, and it is a bug if there are still crashes.
This is reasonable and would be pretty easy to implement, but it raises the question of how many requests should be allowed? Should it allow 5, 10, or 100 requests before returning "thread busy"? Or an arbitrary amount? Or a configurable amount?
My thinking has been that if a client needs a request queue, it is probably better for the client implement the queue, than to have the server provide one because the client should know its own needs better. Also, not every client needs a queue. For example, Sjor's SV2 client is multithreaded and only makes blocking requests. It just creates one server thread for each client thread, so every server thread is guaranteed to not be busy when a request comes in.
Since the rust client is single threaded and nonblocking, it needs to create different server threads for those requests to execute in parallel like the c++ client does, or it needs to have a queue so requests created in parallel can be executed serially on a single server thread.
In practice, I think it make sense for the rust client to just create 2 server threads: a waiting thread to execute
waitNext()calls, and a worker thread using a queue to execute all other calls.It if would help, I could add a
enableRequestQueue()or similar method to theThreadinterface so the rust client can ask for the requests sent to a thread to be queued.However, I think it would probably be straightforward to implement a queue in rust on the client side instead. And I wouldn't want queuing to be enabled on the server by default because it could mask other problems. Like if a request was accidentally sent to the waiting thread rather than the worker thread, it could just sit there forever if queuing was enabled on the waiting thread, instead of returning an error.
Thanks for reporting the problem here and I hope this can lead to a good solution.
plebhash commentedon Nov 22, 2025
I guess the main thing I wanted to report here is a duplicate of bitcoin-core/libmultiprocess#214, so it's good to know these crashes won't happen on
v30.x!it would be up to the client to know what are the reasonable bounds for backpressure so that it can avoid the need to handle errors, so I'd say that a configurable amount with a reasonably large default
but since you also mentioned:
I wouldn't necessarily frame this as an actual feature request, mainly because we're working around this without having to resort to server-side queueing.
currently we're going with the first approach, so now we create 5 server threads, one dedicated for each potentially concurrent call:
submitSolutiongetHeadergetCoinbaseTxgetCoinbaseMerklePathwaitNextplus some ad-hoc threads that are created ephemerally whenever we need to do a
getBlock(either to reply to aRequestTransactionDataor because we want to dump a valid solution to disk)to be honest, we could have gotten away with only 2 by forcing
getHeader,getCoinbaseTx,getCoinbaseMerklePathandwaitNextto be done sequentially under the same server threadbut we're trying to squeeze out every possible micro/nano/pico second out of the way with concurrent execution of
getHeader,getCoinbaseTx,getCoinbaseMerklePathso we can craft aNewTemplatemessage ASAP (the ones for mempool updates are actually rate-limited, so we're not really in a race... but for chain tip updates we do want this to happen as fast as possible)with this approach, queuing requests on the server side is not needed at all.
but it made me wonder about something (which I was eventually going to be forced to consider anyways):
we eventually want to adapt this code so that it can become a sidecar that's attached to Bitcoin Core, which then acts as a Sv2 Template Provider, listening on a TCP port and catering for as many clients as possible... so now, for every client that connects to it, we create 5 new threads on Bitcoin Core
whether we create 1, 2 or 5 threads for each client is not too relevant, but my main question is whether there are there protection mechanisms in place in case we try to create too many threads at once? if so, what happens once Bitcoin Core is no longer willing to create new threads?
Sjors commentedon Nov 24, 2025
I think you should bench this before deciding if it needs optimising and also check whether it's actually faster and not e.g. slowing the node responses down with overhead.
If
getHeader,getCoinbase(Tx)andgetCoinbaseMerklePathreally need to be called in parallel, then I think it would be better if theBlockTemplateinterface is somehow pre-populated with those values - avoiding these round trips entirely.ryanofsky commentedon Dec 3, 2025
re: #33923 (comment)
There aren't any currently, and making a lot of threads is probably the easiest way an IPC client could DoS the node.
It wouldn't be hard to create a limit though, if there is a use-case.
I do think creating 5 threads for each client that needs to receive templates sounds like overkill, and would suggest trying to implement an approach with less overhead. Ideally the SV2 template provider be able to take a single bitcoin core template and broadcast it out to many clients, instead of each client needing unique templates.
I also agree with sjors it doesn't seem ideal to be calling getters like getHeader, getCoinbaseTx, and getCoinbaseMerklePath in parallel. If we can change the interface to provide a single method that will return all the information needed so you don't need to make multiple calls, that would be better.
re: #33923 (comment)
Replying to myself, I now am rethinking my previous thinking. One case where a server queue is clearly better than a client queue is when a client wants to request multiple pieces of data and have the data sent back sequentially. Without a server queue, the client needs to wait for each piece of data to be received before requesting the next piece of data, which is not efficient. With a server queue, data can be streamed back much more nicely. So now I am thinking about ways to implement a queue. And I think it might be possible to do without creating an separate data structure or explicit queue limit by simply attaching data to capnproto promises. This would get rid of the "thread busy" error entirely.
ismaelsadeeq commentedon Jan 19, 2026
re #34250 (comment)
I agree with @Sjors that we should just expose those values in one call to avoid the round trip, @plebhash it would also be nice to benchmark what @Sjors suggests.
I also agree on the potential of throttling if we allow an arbitrary number of threads. It should definitely be rate-limited.
plebhash commentedon Jan 19, 2026
here's a benchmarking suite: https://github.com/plebhash/sv2-bitcoin-core/tree/2026-01-19-benchmark-fetch-template-strategies
the methodology consists of:
collect 100 samples of fetching template data under two different strategies:
getHeader, thengetCoinbaseTx, thengetCoinbaseMerklePath, all against the same pre-allocated server threadgetHeader,getCoinbaseTx, andgetCoinbaseMerklePath, against 3 different pre-allocated server threadsnotes:
v30.2running onmainnetBlockTemplate(I wonder if that could skew or bias the results in any meaningful way?)here's the results:
Allow simultaneous calls on same Context.thread
ryanofsky commentedon Jan 21, 2026
Note: The first commit bitcoin-core/libmultiprocess@8fe9da2 from bitcoin-core/libmultiprocess#240 removes restriction on clients sending many requests at the same time to the same server thread. With that change the server just queues requests and executes them in the order received.
I went ahead and implemented this after realizing the server-side queuing could be implemented by just chaining promises together without needing to add new data structure, and after realizing the client-side queuing I suggested earlier was inherently less efficient because it requires clients to wait to receive responses from previous requests before sending new requests.
There is also some new python code in #34284 testing this behavior.
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Allow simultaneous calls on same Context.thread
Merge #240: Avoid errors from asynchronous (non-c++) clients
Merge #34422: Update libmultiprocess subtree to be more stable with r…
Merge bitcoin/bitcoin#34422: Update libmultiprocess subtree to be mor…
Merge bitcoin/bitcoin#34422: Update libmultiprocess subtree to be mor…
Merge bitcoin/bitcoin#34422: Update libmultiprocess subtree to be mor…
Allow simultaneous calls on same Context.thread