Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public IRubyObject resume(ThreadContext context, IRubyObject[] values) {

FiberRequest result;
try {
result = exchangeWithFiber(context, currentFiberData, data, val);
result = exchangeWithFiber(context, currentFiberData, data, val, true);
} finally {
data.prev = null;
currentFiberData.resumingFiber = null;
Expand Down Expand Up @@ -250,7 +250,8 @@ public static class FiberRequest {

private static final FiberRequest NEVER = new FiberRequest(RubyBasicObject.NEVER, RequestType.DATA);

private static FiberRequest exchangeWithFiber(ThreadContext context, FiberData currentFiberData, FiberData targetFiberData, FiberRequest request) {
private static FiberRequest exchangeWithFiber(ThreadContext context, FiberData currentFiberData, FiberData targetFiberData,
FiberRequest request, boolean checkKilled) {
// MRI: fiber_switch. Must precede the push, which wakes the target fiber's thread
if (currentFiberData.blocking) context.getFiberCurrentThread().decrementBlocking();

Expand Down Expand Up @@ -279,7 +280,7 @@ private static FiberRequest exchangeWithFiber(ThreadContext context, FiberData c
}

// MRI: fiber_check_killed, for a kill that arrived while we were switched away
if (currentFiberData.killed) throw new FiberKill();
if (checkKilled && currentFiberData.killed) throw new FiberKill();

return result;
}
Expand Down Expand Up @@ -364,7 +365,7 @@ public IRubyObject transfer(ThreadContext context, IRubyObject[] values) {

FiberRequest result;
try {
result = exchangeWithFiber(context, currentFiberData, data, val);
result = exchangeWithFiber(context, currentFiberData, data, val, true);
} finally {
currentFiberData.transferred = false;
}
Expand All @@ -376,6 +377,7 @@ public IRubyObject transfer(ThreadContext context, IRubyObject[] values) {
return processResultData(context, result);
}

@CRuby(value = "fiber_raise", file = "cont.c", note = "current and resuming-fiber cases")
public IRubyObject raise(ThreadContext context, IRubyObject exception) {
Ruby runtime = context.runtime;

Expand All @@ -394,17 +396,24 @@ public IRubyObject raise(ThreadContext context, IRubyObject exception) {
ThreadFiber resuming = data.resumingFiber;
if (resuming != null) return resuming.raise(context, exception);

// the backtrace is filled in where the target fiber raises it, as in MRI
return switchToRaise(context, new FiberRequest(exception, RequestType.RAISE));
}

@CRuby(value = "fiber_raise", file = "cont.c", note = "transfer or resume into the fiber")
private IRubyObject switchToRaise(ThreadContext context, FiberRequest val) {
Ruby runtime = context.runtime;
final FiberData data = this.data;

// MRI: a fiber suspended by transfer (not in Fiber.yield) is raised into by transferring to it
boolean transfer = !data.yielding;

if (!transfer && data.prev != null) throw runtime.newFiberError("double resume");

if (data.parent != context.getFiberCurrentThread()) fiberCalledAcrossThreads(runtime);

// the backtrace is filled in where the target fiber raises it, as in MRI
FiberRequest val = new FiberRequest(exception, RequestType.RAISE);

ThreadFiber currentFiber = context.getFiber();
FiberData currentFiberData = currentFiber.data;
if (transfer) {
currentFiberData.transferred = true;
} else {
Expand All @@ -415,7 +424,7 @@ public IRubyObject raise(ThreadContext context, IRubyObject exception) {

FiberRequest result = null;
try {
result = exchangeWithFiber(context, currentFiberData, data, val);
result = exchangeWithFiber(context, currentFiberData, data, val, true);
} finally {
if (transfer) {
currentFiberData.transferred = false;
Expand Down Expand Up @@ -455,7 +464,7 @@ private static IRubyObject exchangeWithPrevFiber(ThreadContext context, Ruby run
FiberRequest result;
currentFiberData.yielding = true;
try {
result = exchangeWithFiber(context, currentFiberData, prevFiberData, request);
result = exchangeWithFiber(context, currentFiberData, prevFiberData, request, true);
} finally {
currentFiberData.yielding = false;
}
Expand Down Expand Up @@ -569,7 +578,7 @@ private static ThreadFiber returnFiber(FiberData data) {

ThreadFiber resuming = fiberData.resumingFiber;
if (resuming == null) {
// only hand over if it is parked in a transfer; an async kill can leave nobody waiting
// only a fiber parked in a transfer is still waiting
return fiberData.transferred ? fiber : null;
}

Expand Down Expand Up @@ -927,7 +936,7 @@ public IRubyObject kill(ThreadContext context) {
if (!alive()) return this;
if (fiberData.started && fiberData.parent != context.getFiberCurrentThread()) fiberCalledAcrossThreads(context.runtime);

// mark before the push, which can block, so a second kill cannot slip in behind it
// mark first, so a kill from the fiber's ensure returns false
fiberData.killed = true;

// MRI: fiber_raise with no exception, forwarded to the fiber this one is resuming. The killed
Expand All @@ -938,7 +947,23 @@ public IRubyObject kill(ThreadContext context) {
return this;
}

fiberData.queue.push(context, new FiberRequest(new FiberKill(), RequestType.RAISE));
// MRI marks an unborn fiber dead without running it; here its thread still has to shut down
if (!fiberData.started) {
ThreadFiber currentFiber = context.getFiber();
fiberData.prev = currentFiber;
try {
// no kill check: MRI does not switch here
FiberRequest kill = new FiberRequest(new FiberKill(), RequestType.RAISE);
FiberRequest result = exchangeWithFiber(context, currentFiber.data, fiberData, kill, false);
if (result.type == RequestType.RAISE) throw raised(context, result);
} finally {
fiberData.prev = null;
}
return this;
}

// MRI: fiber_raise
switchToRaise(context, new FiberRequest(new FiberKill(), RequestType.RAISE));

return this;
}
Expand Down
15 changes: 15 additions & 0 deletions spec/ruby/core/fiber/kill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@
ensure_executed.should == true
end

it "finishes the ensure block when the ensure kills a non-resumed fiber" do
ScratchPad.record []

fiber = Fiber.new do
while true; Fiber.yield; end
ensure
Fiber.new {}.kill
ScratchPad << :after_kill
end

fiber.resume
fiber.kill
ScratchPad.recorded.should == [:after_kill]
end

it "does not execute rescue block" do
rescue_executed = false

Expand Down
9 changes: 0 additions & 9 deletions spec/tags/ruby/core/fiber/kill_tags.txt

This file was deleted.

Loading