Commit 075fbf5
committed
fix: resolve race condition in VMQ device deletion acknowledgment
Device deletion in Astarte involves a distributed coordination mechanism where
the VMQ plugin must acknowledge deletion by setting vmq_ack=true in the database
and sending a /f message to trigger final cleanup. The current implementation has
a critical race condition that can cause device deletions to stall forever.
The root cause is that the ack_device_deletion/2 function executes
operations in the wrong order:
1. Sends /f message to AMQP
2. Writes vmq_ack=true to database
This creates a race condition where:
- /f message is published successfully
- Database write fails (timeout, connection error, etc.)
- DUP processes /f message and sets dup_end_ack=true
- vmq_ack remains false, causing all_ack?() to return false
- Device deletion stalls permanently with no retry mechanism
Additionally, the RPC server always returns :ok regardless of actual operation
success, masking errors from callers.
The involves:
1. Reordering operations to ensure database write completes before
message publication
2. Using proper error handling with early return on database failures
3. Propagating errors correctly through RPC server to enable caller
retry logic
This ensures atomic behavior: either both operations succeed or both fail,
eliminating the race condition that caused permanent deletion stalls.
Breaking change: RPC callers will now receive error responses for failed
operations instead of false success indicators. However, error cases are
already handled in Astarte since v1.2:
https://github.com/astarte-platform/astarte/blob/35c877efeece31a66576982f9fa30c00b4b801ea/apps/astarte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/impl.ex#L2340
Signed-off-by: Davide Briani <davide.briani@secomind.com>1 parent 2abcf21 commit 075fbf5
File tree
3 files changed
+26
-9
lines changed- lib
- astarte_vmq_plugin/rpc
3 files changed
+26
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
| 186 | + | |
185 | 187 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
191 | 195 | | |
192 | 196 | | |
193 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
75 | 76 | | |
76 | | - | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
79 | 90 | | |
80 | 91 | | |
81 | 92 | | |
| |||
0 commit comments