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
Process-wide barrier implemented as thread_get_state sometimes hangs in Rosetta #133797
thread_get_state appears to be fairly fragile when called under Rosetta emulation.
The implementation needs to reconstruct x64 register state from the actual arm64 state and it is possibly a fairly complex routine. Possibly involves taking kernel locks - I can only guess.
The end result is that, depending on the state of other threads, the caller and the target thread both could hang.
Examples of the vulnerable thread states:
another thread does another process-wide-barrier call (not sure it must be the target thread or any other thread).
Taking a global lock around calling PW barrier can mitigate this scenario.
another thread is forking.
This is more rare. The global lock does not help here.
@VSadov do you have a repro? I'd love to take a look at it. The old GC deadlock from the other issue doesn't repro for me in the recent main.
I just asked copilot to make a repro and it did - and proposed a series of fixes.
I did not keep the repros - there were several as this bug has layers.
I think more context could be useful:
I have hit this in VSadov/Satori#85 . As a part of merge from upstream I have made Satori to be buildable/runnable in Rosetta, as people asked for that, but stress testing was hitting hangs in PW barrier.
the first layer of this bug is reachable when the barrier is called concurrently on more than one thread.
The first fix proposed was to switch to mprotect scheme that works on x64. It "worked" in Rosetta, but for wrong reasons.
Under Rosetta the actual OS is still arm64 and mprotect does not interrupt remote cores. Turned out that it is just an expensive syscall that delays the caller long enough that remote cores drain their writes.
The second fix proposal was to add a global lock around WB barrier. That worked for the first layer of the bug
more stressing later has hit another layer of this bug - the caller hangs when the target thread is forking.
The proposed fix was to switch to a handshake scheme via signals - send signals to all threads and in the handler interlock-decrement a counter, while caller waits for the count to get to zero.
The approach kinda worked, but was uselessly slow.
The next refinement was to not signal threads that were not runnable at the time of iterating. The idea is if they get to run, they'd have buffers effectively flushed. These were also threads that were very slow to respond to signals.
Filtering only runnable made the barrier a lot faster, but still pretty slow. I think there were also some stress concerns about state of the threads we are signaling.
That was the point where I decided that this went too far into the woods. The implementation was getting more and more complex and performance still not even near the desirable latency - the barrier still took up to 3 milliseconds, which is easily several times longer than entire blocking GC when Satori runs in low latency mode.
So I stopped going on that path.
FWIW, the most performing and the most reliable way that I found is to simply wait in the caller for 5 microseconds. And that fix went into VSadov/Satori#89
The solution relies on nothing that is guaranteed architecturally, just on the fact that Rosetta runs in TSO mode and on passing of time.
Although it appears that "passing of time" is a quite reliable thing. :-)
I am curious if better solution can be found here.
another layer of this bug - the caller hangs when the target thread is forking.
This is what doesn't seem to happen in main, I believe there were changes that fixed the issue. Maybe you've been running satori with older runtime state?
another layer of this bug - the caller hangs when the target thread is forking.
This is what doesn't seem to happen in main, I believe there were changes that fixed the issue. Maybe you've been running satori with older runtime state?
The VM state was basically the point when dotnet/net11 was forked off main - that is my current net11 base in Satori. Unless there were further fixes in this area after that point, I'd expect the hang is still reachable. Perhaps hard to reach.
It took a while to hit this. I even merged VSadov/Satori#85 thinking it was clean.
However, when testing unrelated changes later there were still occasional, although much less frequent than before, hangs.
another layer of this bug - the caller hangs when the target thread is forking.
As I remember the agent experimented a bit and found other states of the remote thread when it was "untouchable" by the thread_get_state, although they were more exotic - like someone suspending a thread or some weird scenarios with exceptions.
The running theory is that thread_get_state may indirectly take some kind of lock (in kernel or in Rosetta) that some other operations related to threads/contexts may take as well. Not sure if that is true.
For years, we have seen a trickle of difficult-to-reproduce crashes and hangs under Rosetta that appear to be Rosetta-related. We have not been able to root cause them. Instead, our recommendation has been to run native arm64 binaries to avoid these reliability issues.
For years, we have seen a trickle of difficult-to-reproduce crashes and hangs under Rosetta that appear to be Rosetta-related. We have not been able to root cause them. Instead, our recommendation has been to run native arm64 binaries to avoid these reliability issues.
I suspect the main reason that osx-x64 and Rosetta are interesting is that osx-x64 is still a thing as a target platform for app stores and such. However, the development is often under Rosetta. (just my guess)
On my part - the hangs were a stress nuisance requiring a fix.
@VSadov I think the change that fixed the hangs with spawning new process was #126063. It seems to come in after the changes you have in the satori branch.
@VSadov I think the change that fixed the hangs with spawning new process was #126063. It seems to come in after the changes you have in the satori branch.
Hmm. Let me check.
It is possible when I fixed the process-wide hang for the second time, I only saw the hang on net10, but used the same fix in both net10 and net11.
I think I'd test if it was already fixed in net11 - in case there is a known fix, only needed to backport, but possibly I didn't since it was the second time the same thing, so I just already assumed there is no existing fix.
Activity
dotnet-policy-service commentedon Sep 12, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Sep 12, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
VSadov commentedon Sep 12, 2026
Yes, the issue that the bot has linked is likely related.
VSadov commentedon Sep 12, 2026
A simple repro could be just running multiple threads doing PW barrier in a loop. That hangs almost immediately in Rosetta.
janvorli commentedon Sep 14, 2026
@VSadov do you have a repro? I'd love to take a look at it. The old GC deadlock from the other issue doesn't repro for me in the recent main.
VSadov commentedon Sep 14, 2026
I just asked copilot to make a repro and it did - and proposed a series of fixes.
I did not keep the repros - there were several as this bug has layers.
I think more context could be useful:
I have hit this in VSadov/Satori#85 . As a part of merge from upstream I have made Satori to be buildable/runnable in Rosetta, as people asked for that, but stress testing was hitting hangs in PW barrier.
the first layer of this bug is reachable when the barrier is called concurrently on more than one thread.
The first fix proposed was to switch to
mprotectscheme that works on x64. It "worked" in Rosetta, but for wrong reasons.Under Rosetta the actual OS is still arm64 and
mprotectdoes not interrupt remote cores. Turned out that it is just an expensive syscall that delays the caller long enough that remote cores drain their writes.The second fix proposal was to add a global lock around WB barrier. That worked for the first layer of the bug
more stressing later has hit another layer of this bug - the caller hangs when the target thread is forking.
The proposed fix was to switch to a handshake scheme via signals - send signals to all threads and in the handler interlock-decrement a counter, while caller waits for the count to get to zero.
The approach kinda worked, but was uselessly slow.
The next refinement was to not signal threads that were not runnable at the time of iterating. The idea is if they get to run, they'd have buffers effectively flushed. These were also threads that were very slow to respond to signals.
Filtering only runnable made the barrier a lot faster, but still pretty slow. I think there were also some stress concerns about state of the threads we are signaling.
That was the point where I decided that this went too far into the woods. The implementation was getting more and more complex and performance still not even near the desirable latency - the barrier still took up to 3 milliseconds, which is easily several times longer than entire blocking GC when Satori runs in low latency mode.
So I stopped going on that path.
FWIW, the most performing and the most reliable way that I found is to simply wait in the caller for 5 microseconds. And that fix went into VSadov/Satori#89
Some more info in comments:
https://github.com/VSadov/Satori/blob/bc478e489d3de376f6c98d0b95dc64beff00a9de/src/native/minipal/memorybarrierprocesswide.c#L149
The solution relies on nothing that is guaranteed architecturally, just on the fact that Rosetta runs in TSO mode and on passing of time.
Although it appears that "passing of time" is a quite reliable thing. :-)
I am curious if better solution can be found here.
janvorli commentedon Sep 14, 2026
This is what doesn't seem to happen in main, I believe there were changes that fixed the issue. Maybe you've been running satori with older runtime state?
janvorli commentedon Sep 14, 2026
Btw, the method to do the barrier using thread_get_state was suggested by Apple kernel engineers, we had a call about it.
VSadov commentedon Sep 14, 2026
The VM state was basically the point when dotnet/net11 was forked off main - that is my current net11 base in Satori. Unless there were further fixes in this area after that point, I'd expect the hang is still reachable. Perhaps hard to reach.
It took a while to hit this. I even merged VSadov/Satori#85 thinking it was clean.
However, when testing unrelated changes later there were still occasional, although much less frequent than before, hangs.
VSadov commentedon Sep 14, 2026
It evidently works reliably in native arm64. I guess Rosetta adds another angle that was not considered.
VSadov commentedon Sep 14, 2026
As I remember the agent experimented a bit and found other states of the remote thread when it was "untouchable" by the thread_get_state, although they were more exotic - like someone suspending a thread or some weird scenarios with exceptions.
The running theory is that thread_get_state may indirectly take some kind of lock (in kernel or in Rosetta) that some other operations related to threads/contexts may take as well. Not sure if that is true.
jkotas commentedon Sep 14, 2026
For years, we have seen a trickle of difficult-to-reproduce crashes and hangs under Rosetta that appear to be Rosetta-related. We have not been able to root cause them. Instead, our recommendation has been to run native arm64 binaries to avoid these reliability issues.
VSadov commentedon Sep 14, 2026
I suspect the main reason that osx-x64 and Rosetta are interesting is that osx-x64 is still a thing as a target platform for app stores and such. However, the development is often under Rosetta. (just my guess)
On my part - the hangs were a stress nuisance requiring a fix.
janvorli commentedon Sep 15, 2026
@VSadov I think the change that fixed the hangs with spawning new process was #126063. It seems to come in after the changes you have in the satori branch.
VSadov commentedon Sep 15, 2026
Hmm. Let me check.
It is possible when I fixed the process-wide hang for the second time, I only saw the hang on net10, but used the same fix in both net10 and net11.
I think I'd test if it was already fixed in net11 - in case there is a known fix, only needed to backport, but possibly I didn't since it was the second time the same thing, so I just already assumed there is no existing fix.
Serialize process-wide memory barriers under Rosetta (#134384)