otp icon indicating copy to clipboard operation
otp copied to clipboard

Fix BEAM crash from from port/NIF thread

Open dotsimon opened this issue 1 year ago • 6 comments

This patch prevents non-scheduler threads from crashing the emulator. Fixes #8208

dotsimon avatar Feb 29 '24 23:02 dotsimon

CT Test Results

    3 files    141 suites   49m 25s ⏱️ 1 602 tests 1 553 ✅ 49 💤 0 ❌ 2 311 runs  2 242 ✅ 69 💤 0 ❌

Results for commit 698fe88c.

:recycle: This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

github-actions[bot] avatar Feb 29 '24 23:02 github-actions[bot]

Rebased onto OTP-23.3.4 + 184634a23120fde29029bc7a765c45838f19c510 which is mergeable to all newer release branches.

Also tweaked solution with a global atomic to get some "randomness".

sverker avatar Mar 04 '24 13:03 sverker

The sample I attached to #8208 (and now it is actually attached!) includes a rudimentary benchmark. There's a lot of variance in the results but they seem to consistently be a few % slower with Sverker's tweak. And in that simple test there is also no contention for the new atomic which I imagine would further degrade performance. So how important is that "randomness"?

dotsimon avatar Mar 05 '24 00:03 dotsimon

What if you change erts_sched_local_random_nosched_state to an Uint and just do

rand_state = erts_sched_local_random_nosched_state++;

sverker avatar Mar 05 '24 11:03 sverker

If something like this was what you had in mind then it was about the same.

dotsimon avatar Mar 05 '24 23:03 dotsimon

Given that the random number is used to pick a random pivot element in quicksort, performance is expected to vary as a result of this change, and the consistently worse performance might just be bad luck (or rather, very good luck with the original implementation always returning a good constant).

jhogberg avatar Mar 06 '24 07:03 jhogberg