Commit 831aaef
committed
Fix lock ordering in dist_nifs.c to prevent SMP deadlocks
https://ampcode.com/threads/T-019ba265-8657-732e-a85b-9871a0214ae2
Fix lock ordering inversions in `dist_nifs.c` that can cause deadlocks under SMP when distribution connections are used concurrently with process termination.
## Problem
Similar to the recently fixed monitor/demonitor deadlock, `dist_nifs.c` calls `enif_demonitor_process` while holding the `remote_monitors` lock. Since `enif_demonitor_process` acquires `monitors` → `processes_table`, this creates an ABBA deadlock risk:
| Code Path | Lock Order |
|-----------|------------|
| `dist_connection_dtor` | `remote_monitors` → `monitors` → `processes_table` |
| `OPERATION_DEMONITOR_P` | `remote_monitors` → `monitors` → `processes_table` |
| `context_destroy` (firing resource monitors) | `processes_table` → `monitors` |
When a distribution connection is being destroyed while a monitored process is terminating, these paths can deadlock.
## Solution
Release the `remote_monitors` lock before calling `enif_demonitor_process`:
1. **`dist_connection_dtor`**: Move all monitors to a local list, unlock, then iterate and demonitor
2. **`OPERATION_DEMONITOR_P`**: Remove the found monitor from the list, unlock, then demonitor
This ensures consistent lock ordering: `processes_table` → `monitors` is always acquired before any synclist locks that might call back into the monitor/demonitor APIs.
Signed-off-by: Peter M <petermm@gmail.com>1 parent 64badd3 commit 831aaef
1 file changed
+13
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
120 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
121 | 128 | | |
122 | 129 | | |
123 | 130 | | |
124 | 131 | | |
125 | 132 | | |
126 | | - | |
127 | 133 | | |
128 | 134 | | |
129 | 135 | | |
| |||
531 | 537 | | |
532 | 538 | | |
533 | 539 | | |
| 540 | + | |
534 | 541 | | |
535 | 542 | | |
536 | 543 | | |
537 | 544 | | |
538 | 545 | | |
539 | 546 | | |
540 | 547 | | |
541 | | - | |
542 | 548 | | |
543 | | - | |
| 549 | + | |
544 | 550 | | |
545 | 551 | | |
546 | 552 | | |
547 | 553 | | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
548 | 558 | | |
549 | 559 | | |
550 | 560 | | |
| |||
0 commit comments