Skip to content

Commit 3c22a9e

Browse files
committed
task_scheduler: Don't allow awaiting from Io Scheduler's task.
This is dangerous because the main task calls IoScheduler::await frequently. If the main task waits for the Io Scheduler and the scheduler waits for the main task, we deadlock.
1 parent 7655d4b commit 3c22a9e

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

software/src/modules/io_scheduler/io_scheduler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ class IoScheduler final : public IModule
112112
}
113113
}
114114

115+
[[gnu::always_inline]]
116+
void assert_task_inactive(const char *error_message) const {
117+
if (xTaskGetCurrentTaskHandle() == task_handle) {
118+
esp_system_abort(error_message);
119+
}
120+
}
121+
115122
private:
116123
[[noreturn]] void task_loop();
117124
[[noreturn]] static void task_fn(void *arg);

software/src/modules/task_scheduler/module.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Requires = Event Log
44
Task Scheduler
55

66
Optional = Debug
7+
Io Scheduler
78

89
; The task scheduler has no dependencies before the loop
910
; and starts the handling of the pre_reboot phase

software/src/modules/task_scheduler/task_scheduler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ bool TaskScheduler::await(uint64_t task_id, millis_t millis_to_wait)
374374
if (strcmp(pcTaskGetName(nullptr), TCPIP_THREAD_NAME) == 0) {
375375
esp_system_abort("Calling TaskScheduler::await is not allowed in the TCP/IP thread!");
376376
}
377+
#if MODULE_IO_SCHEDULER_AVAILABLE()
378+
// This is dangerous because the main task calls IoScheduler::await frequently.
379+
// If the main task waits for the Io Scheduler and the scheduler waits for the main task, we deadlock.
380+
io_scheduler.assert_task_inactive("Calling TaskScheduler::await is not allowed in the Io Scheduler thread!");
381+
#endif
377382
#endif
378383
if (task_id == 0) {
379384
logger.printfln("Attempted to await task 0");

0 commit comments

Comments
 (0)