Skip to content

Scheduler.await termination delegate accesses non valid data #644

@matthias-wende-sociomantic

Description

This problem occurs during shutting down the application after calling theScheduler.shutdown and causes the app to SEGFAULT. Note that it is tested in D2 only.

It seems that in at least one case the context variable is not valid when accessed from the delegate.
https://github.com/sociomantic-tsunami/ocean/blob/v4.x.x/src/ocean/task/Scheduler.d#L430

Here is the stacktrace:

#0  0x000000000065c7bc in _D5ocean4task4TaskQf9suspendedMFZb (this=0x0) at ./submodules/ocean/src/ocean/task/Task.d:399
#1  0x0000000000696245 in _D5ocean4task9SchedulerQk5awaitMFCQBgQBd4TaskQfMDFQrZvZ9__lambda3MFZv (this=0x7f49a3e4ecd8)
    at ./submodules/ocean/src/ocean/task/Scheduler.d:412
#2  0x000000000065cb8f in _D5ocean4task4TaskQf10entryPointMFZb (this=0x7f499f966d00) at ./submodules/ocean/src/ocean/task/Task.d:489
#3  0x00000000005f247d in _D5ocean4task8internal18FiberPoolWithQueueQu17workerFiberMethodMFZ7runTaskMFZv (this=0x7f4998325f88)
    at ./submodules/ocean/src/ocean/task/internal/FiberPoolWithQueue.d:175
#4  0x00000000005f23ba in _D5ocean4task8internal18FiberPoolWithQueueQu17workerFiberMethodMFZv (this=0x7f49a3e58e00)
    at ./submodules/ocean/src/ocean/task/internal/FiberPoolWithQueue.d:189
#5  0x00000000006d3848 in core.thread.Fiber.run() (this=0x7f499f965a00) at src/core/thread.d:4389
#6  0x00000000006d3717 in fiber_entryPoint () at src/core/thread.d:3626
#7  0x0000000000000000 in ?? ()

Exchanging the delegate with a closure as follows fixes the problem but seems not to be a reasonable solution.

auto cl = {
    if (context.suspended())
        theScheduler.delayedResume(context);
};
// this methods stack is guaranteed to still be valid by the time
// task finishes, so we can reference `task` from delegate
task.terminationHook(cl);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions