Skip to content

Commit

Permalink
isfatal
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Apr 17, 2016
1 parent f4cb131 commit 706fe28
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ end
function eval_user_input(ast::ANY, backend::REPLBackend)
iserr, lasterr, bt = false, (), nothing
while true
Base.set_fatal_eh()
try
if iserr
put!(backend.response_channel, (lasterr, bt))
Expand Down
11 changes: 11 additions & 0 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ macro assert(ex, msgs...)
end


isfatal(error) = false
if isdefined(Main, :Base)
isfatal(::StackOverflowError) = true
isfatal(::OutOfMemoryError) = true
isfatal(::UndefVarError) = true
end

set_fatal_eh() = ccall(:jl_set_fatal_eh, Void, ())
rethrow_fatal() = ccall(:jl_rethrow_fatal, Void, ())


"""
retry(f, [condition]; n=3; max_delay=10) -> Function
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ export
@catch,
catch_backtrace,
error,
isfatal,
rethrow,
retry,
systemerror,
Expand Down
8 changes: 6 additions & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3078,8 +3078,9 @@ f(x) = yt(x)
;; (enter L) - push handler with catch block at label L
;; (leave n) - pop N exception handlers
((trycatch)
(let ((catch (make-label))
(endl (make-label)))
(let ((catch (make-label))
(notfatal (make-label))
(endl (make-label)))
(emit `(enter ,catch))
(set! handler-level (+ handler-level 1))
(let* ((v1 (compile (cadr e)
Expand All @@ -3095,6 +3096,9 @@ f(x) = yt(x)
(set! handler-level (- handler-level 1))
(mark-label catch)
(emit `(leave 1))
(emit `(gotoifnot ,`(call (top isfatal) ,'(the_exception)) ,notfatal))
(emit `(call (top rethrow_fatal)))
(mark-label notfatal)
(let ((v2 (compile (caddr e) break-labels value tail)))
(if val (emit `(= ,val ,v2)))
(if endl (mark-label endl))
Expand Down
4 changes: 4 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,8 @@ typedef struct _jl_task_t {

// current exception handler
jl_handler_t *eh;
// fatal exception handler
jl_handler_t *fatal_eh;
// saved gc stack top for context switches
jl_gcframe_t *gcstack;
// current module, or NULL if this task has not set one
Expand Down Expand Up @@ -1434,6 +1436,8 @@ JL_DLLEXPORT jl_value_t *jl_switchto(jl_task_t *t, jl_value_t *arg);
JL_DLLEXPORT void JL_NORETURN jl_throw(jl_value_t *e);
JL_DLLEXPORT void JL_NORETURN jl_rethrow(void);
JL_DLLEXPORT void JL_NORETURN jl_rethrow_other(jl_value_t *e);
JL_DLLEXPORT void jl_set_fatal_eh(void);
JL_DLLEXPORT void jl_rethrow_fatal(void);

#ifdef JULIA_ENABLE_THREADING
STATIC_INLINE void jl_lock_frame_push(void (*unlock_func)(void))
Expand Down
14 changes: 14 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,18 @@ JL_DLLEXPORT void jl_rethrow(void)
throw_internal(jl_exception_in_transit);
}

JL_DLLEXPORT void jl_set_fatal_eh(void)
{
jl_current_task->fatal_eh = jl_current_task->eh;
}

JL_DLLEXPORT void jl_rethrow_fatal(void)
{
if (jl_current_task->eh != jl_current_task->fatal_eh) {
jl_rethrow();
}
}

JL_DLLEXPORT void jl_rethrow_other(jl_value_t *e)
{
throw_internal(e);
Expand Down Expand Up @@ -554,6 +566,7 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, size_t ssize)
t->backtrace = jl_nothing;
// there is no active exception handler available on this stack yet
t->eh = NULL;
t->fatal_eh = NULL;
t->gcstack = NULL;
t->stkbuf = NULL;
t->tid = 0;
Expand Down Expand Up @@ -661,6 +674,7 @@ void jl_init_root_task(void *stack, size_t ssize)
jl_current_task->exception = jl_nothing;
jl_current_task->backtrace = jl_nothing;
jl_current_task->eh = NULL;
jl_current_task->fatal_eh = NULL;
jl_current_task->gcstack = NULL;
jl_current_task->tid = ti_tid;
#ifdef JULIA_ENABLE_THREADING
Expand Down

0 comments on commit 706fe28

Please sign in to comment.