Skip to content

Commit 0cd607b

Browse files
committed
rt: Shutdown gracefully on failure
When the kernel fails, kill all tasks and wait for the schedulers to stop instead of just exiting. I'm sure there are tons of lurking issues here but this is enough to fail without leaking (at least in the absence of cleanups).
1 parent f6ad051 commit 0cd607b

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

src/rt/rust_kernel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ int rust_kernel::start_task_threads()
134134
return rval;
135135
}
136136

137+
void
138+
rust_kernel::fail() {
139+
for(size_t i = 0; i < num_threads; ++i) {
140+
rust_scheduler *thread = threads[i];
141+
thread->kill_all_tasks();
142+
}
143+
}
144+
137145
rust_task_id
138146
rust_kernel::create_task(rust_task *spawner, const char *name) {
139147
rust_scheduler *thread = threads[rand(&rctx) % num_threads];

src/rt/rust_kernel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class rust_kernel {
5252
void *realloc(void *mem, size_t size);
5353
void free(void *mem);
5454

55+
void fail();
56+
5557
int start_task_threads();
5658

5759
#ifdef __WIN32__

src/rt/rust_scheduler.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,21 @@ rust_scheduler::fail() {
7171
name, this);
7272
I(this, kernel->rval == 0);
7373
kernel->rval = 1;
74-
exit(1);
74+
kernel->fail();
75+
}
76+
77+
void
78+
rust_scheduler::kill_all_tasks() {
79+
I(this, !lock.lock_held_by_current_thread());
80+
scoped_lock with(lock);
81+
82+
for (size_t i = 0; i < running_tasks.length(); i++) {
83+
running_tasks[i]->kill();
84+
}
85+
86+
for (size_t i = 0; i < blocked_tasks.length(); i++) {
87+
blocked_tasks[i]->kill();
88+
}
7589
}
7690

7791
size_t

src/rt/rust_scheduler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct rust_scheduler : public kernel_owned<rust_scheduler>,
8181

8282
void log_state();
8383

84+
void kill_all_tasks();
85+
8486
rust_task *create_task(rust_task *spawner, const char *name);
8587

8688
virtual void run();

src/rt/rust_upcall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ upcall_fail(rust_task *task,
195195
size_t line) {
196196
LOG_UPCALL_ENTRY(task);
197197
LOG_ERR(task, upcall, "upcall fail '%s', %s:%" PRIdPTR, expr, file, line);
198-
task->fail();
199198
task->die();
199+
task->fail();
200200
task->notify_tasks_waiting_to_join();
201201
task->yield(4);
202202
}

0 commit comments

Comments
 (0)