Skip to content

Conversation

@laytan
Copy link
Collaborator

@laytan laytan commented Nov 23, 2025

This was causing deadlocks because:

  1. The main thread would thread.terminate a running thread
  2. thread.terminate does not mean termination happens immediately
  3. The thread that was terminated would see that the main thread released print_mutex and acquire it
  4. The worker would execute fmt.printf which is a cancellation point
  5. Cancellation point reached, cancelled
  6. Deadlock because the thread was holding print_mutex

You would usually solve this with pthread_cleanup_push that would release the mutex in case it is cancelled, or use
pthread_setcancelstate to disable cancellation while the mutex is held. But the real fix is just not using forced termination and using a flag or other mechanism to indicate to the thread it should stop.

thread.terminate shouldn't even be a thing IMO.
It is way to dangerous and if somebody knows what they are doing they can use the core:sys procedures to achieve it.

And we certainly shouldn't be using it in the demo as an example.

This was causing deadlocks because:

1. The main thread would `thread.terminate` a running thread
2. `thread.terminate` does not mean termination happens immediately
3. The thread that was terminated would see that the main thread
   released `print_mutex` and acquire it
4. The worker would execute `fmt.printf` which is a cancellation point
5. Cancellation point reached, cancelled
6. Deadlock because the thread was holding `print_mutex`

You would usually solve this with `pthread_cleanup_push` that would
release the mutex in case it is cancelled, or use
`pthread_setcancelstate` to disable cancellation while the mutex is
held. But the real fix is just not using forced termination and using a
flag or other mechanism to indicate to the thread it should stop.

`thread.terminate` shouldn't even be a thing IMO.
It is way to dangerous and if somebody knows what they are doing they
can use the core:sys procedures to achieve it.

And we certainly shouldn't be using it in the demo as an example.
@Kelimion Kelimion merged commit 97e423f into odin-lang:master Nov 23, 2025
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants