Skip to content

Commit

Permalink
fix interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
pfitzseb committed Jan 26, 2018
1 parent 182db9d commit 20bba07
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions src/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,56 +80,66 @@ function didWriteToREPL(f)
rout, wout = redirect_stdout()
rerr, werr = redirect_stderr()

ct = current_task()

didWrite = false

outreader = @async begin
didWriteLinebreak = false
while isopen(rout)
if isopen(rout)
try
while isopen(rout)
r = readavailable(rout)
didRead = length(r) > 0
if !didWrite && didRead
print(origout, "\r \r")
end
didWrite |= didRead
write(origout, r)
end

if didRead
didWriteLinebreak = r[end] == 0x0a
if didRead
didWriteLinebreak = r[end] == 0x0a
end
end
catch e
Base.throwto(ct, e)
rethrow(e)
end
didWriteLinebreak
end

errreader = @async begin
didWriteLinebreak = false
while isopen(rerr)
r = readavailable(rerr)
didRead = length(r) > 0
if !didWrite && didRead
print(origout, "\r \r")
end
didWrite |= didRead
write(origerr, r)
try
while isopen(rerr)
r = readavailable(rerr)
didRead = length(r) > 0
if !didWrite && didRead
print(origout, "\r \r")
end
didWrite |= didRead
write(origerr, r)

if didRead
didWriteLinebreak = r[end] == 0x0a
if didRead
didWriteLinebreak = r[end] == 0x0a
end
end
catch e
Base.throwto(ct, e)
rethrow(e)
end
didWriteLinebreak
end

local res
didWriteLinebreakOut, didWriteLinebreakErr = false, false

try
res = f()
finally
redirect_stdout(origout)
redirect_stderr(origerr)
close(wout)
close(werr)
res = f()
redirect_stdout(origout)
redirect_stderr(origerr)
close(wout)
close(werr)

# FIXME: This probably leaks unterminated Tasks everywhere...
if !(res isa EvalError)
didWriteLinebreakOut = wait(outreader)
didWriteLinebreakErr = wait(errreader)
end
Expand Down

0 comments on commit 20bba07

Please sign in to comment.