Repository navigation
Fix order of messages sent from tasks - #2423
Conversation
|
test_send_message_from_task_order and its call may need to be guarded with AVM_TASK_DRIVER_ENABLED |
|
ran into same bug just now, just need the test guards afaik: Review: AtomVM PR #2423PR: Fix order of messages sent from tasks Verdict: correct runtime fix; guard the test before merging. Reversing the Required: support task-driver-disabled builds
--- a/tests/test-mailbox.c
+++ b/tests/test-mailbox.c
@@
+#ifdef AVM_TASK_DRIVER_ENABLED
void test_send_message_from_task_order(void)
{
@@
context_destroy(ctx);
globalcontext_destroy(glb);
}
+#endif
int main(int argc, char **argv)
@@
test_mailbox_send();
test_mailbox_next();
+#ifdef AVM_TASK_DRIVER_ENABLED
test_send_message_from_task_order();
+#endifSuggested test: preserve order across successive drainsThe current test covers one batch. Queue another before consuming the mailbox @@ test_send_message_from_task_order
globalcontext_process_task_driver_queues(glb);
+#ifndef AVM_NO_SMP
+ smp_spinlock_lock(&glb->processes_spinlock);
+#endif
+ for (int i = 4; i <= 6; i++) {
+ globalcontext_send_message_from_task(glb, ctx->process_id, NormalMessage, term_from_int(i));
+ }
+#ifndef AVM_NO_SMP
+ smp_spinlock_unlock(&glb->processes_spinlock);
+#endif
+ globalcontext_process_task_driver_queues(glb);
assert(mailbox_process_outer_list_native(&ctx->mailbox) == NULL);
- for (int i = 1; i <= 3; i++) {
+ for (int i = 1; i <= 6; i++) {Scope and evidence
|
The STA handler sets sta_state to connected when it gets an IP; the Ethernet one did not, so a late eth_started could leave eth_status/0 at started after wait_for_eth/0 had returned the address. That failed test_eth_teardown on the ESP32-S3 with USB CDC, where the scheduler is busier and task messages can still arrive out of order until atomvm#2423. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
|
Thanks both. Pushed c784075: the test and its call are under Checked on generic_unix: it passes with the fix and fails without it (ASan build), and it builds and passes with |
c784075 to
a002b86
Compare
bettio
left a comment
There was a problem hiding this comment.
We are super close to merging this PR.
I suggest also squashing the 2 commits of this PR into a single one: the first commit fails to build with -DAVM_DISABLE_TASK_DRIVER=ON.
|
Thanks. Squashed into a single commit (761e200, force-pushed) with both suggestions:
test-mailbox passes with ASan on the default build, with |
a002b86 to
761e200
Compare
Fix order of messages sent from tasks These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
The STA handler sets sta_state to connected when it gets an IP; the Ethernet one did not, so a late eth_started could leave eth_status/0 at started after wait_for_eth/0 had returned the address. That failed test_eth_teardown on the ESP32-S3 with USB CDC, where the scheduler is busier and task messages can still arrive out of order until atomvm#2423. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
The STA handler sets sta_state to connected when it gets an IP; the Ethernet one did not, so a late eth_started could leave eth_status/0 at started after wait_for_eth/0 had returned the address. That failed test_eth_teardown on the ESP32-S3 with USB CDC, where the scheduler is busier and task messages can still arrive out of order until atomvm#2423. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
The STA handler sets sta_state to connected when it gets an IP; the Ethernet one did not, so a late eth_started could leave eth_status/0 at started after wait_for_eth/0 had returned the address. That failed test_eth_teardown on the ESP32-S3 with USB CDC, where the scheduler is busier and task messages can still arrive out of order until atomvm#2423. Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com> (cherry picked from commit 00961d8) (cherry picked from commit 36a288a)
globalcontext_send_message_from_taskpushes onto the head of the global message queue when it cannot enqueue directly, andglobalcontext_process_message_queuedrained it from the head, so messages a task sent in a row reached the mailbox in reverse order.Without SMP every message from a task goes through this queue. Found on the ESP32-C3 in QEMU while testing Ethernet (#2419): the network driver posts
eth_startedtheneth_connected, and the network process received them the other way round, sonetwork:eth_status/0stayedstarted. With SMP the same happens whenever the process table or the processes spinlock is busy.The fix reverses the queue before enqueuing.
test-mailboxgets a test that sends 1, 2, 3 from a task with the spinlock held (which forces the queued path with SMP too) and checks they arrive in that order; it fails without the fix.Tested: test-mailbox, test-structs and test-erlang on generic_unix (ASan); the ESP32-C3 QEMU suite of #2419 passes with it.
These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later