Skip to content

Commit 3c33910

Browse files
committed
Print backtraces from user-facing JL_CATCH
Since `jlbacktrace` uses `STDERR_FILENO` directly we have to be a bit careful to avoid mangled errors (particularily on Windows), so we use `STDERR_FILENO` directly, when also using `jlbacktrace`.
1 parent 7f518ef commit 3c33910

File tree

7 files changed

+43
-36
lines changed

7 files changed

+43
-36
lines changed

src/dump.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -2072,11 +2072,12 @@ static void jl_reinit_item(jl_value_t *v, int how, arraylist_t *tracee_list)
20722072
}
20732073
}
20742074
JL_CATCH {
2075-
jl_printf(JL_STDERR, "WARNING: error while reinitializing value ");
2076-
jl_static_show(JL_STDERR, v);
2077-
jl_printf(JL_STDERR, ":\n");
2078-
jl_static_show(JL_STDERR, jl_current_exception());
2079-
jl_printf(JL_STDERR, "\n");
2075+
jl_printf((JL_STREAM*)STDERR_FILENO, "WARNING: error while reinitializing value ");
2076+
jl_static_show((JL_STREAM*)STDERR_FILENO, v);
2077+
jl_printf((JL_STREAM*)STDERR_FILENO, ":\n");
2078+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
2079+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
2080+
jlbacktrace(); // written to STDERR_FILENO
20802081
}
20812082
}
20822083

src/gc.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,10 @@ static void run_finalizer(jl_ptls_t ptls, jl_value_t *o, jl_value_t *ff)
277277
jl_get_ptls_states()->world_age = last_age;
278278
}
279279
JL_CATCH {
280-
jl_printf(JL_STDERR, "error in running finalizer: ");
281-
jl_static_show(JL_STDERR, jl_current_exception());
282-
jl_printf(JL_STDERR, "\n");
280+
jl_printf((JL_STREAM*)STDERR_FILENO, "error in running finalizer: ");
281+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
282+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
283+
jlbacktrace(); // writen to STDERR_FILENO
283284
}
284285
}
285286

src/gf.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ void jl_call_tracer(tracer_cb callback, jl_value_t *tracee)
6060
}
6161
JL_CATCH {
6262
ptls->in_pure_callback = last_in;
63-
jl_printf(JL_STDERR, "WARNING: tracer callback function threw an error:\n");
64-
jl_static_show(JL_STDERR, jl_current_exception());
65-
jl_printf(JL_STDERR, "\n");
66-
jlbacktrace();
63+
jl_printf((JL_STREAM*)STDERR_FILENO, "WARNING: tracer callback function threw an error:\n");
64+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
65+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
66+
jlbacktrace(); // written to STDERR_FILENO
6767
}
6868
}
6969

@@ -300,9 +300,9 @@ jl_code_info_t *jl_type_infer(jl_method_instance_t *mi, size_t world, int force)
300300
src = (jl_code_info_t*)jl_apply(fargs, 3);
301301
}
302302
JL_CATCH {
303-
jl_printf(JL_STDERR, "Internal error: encountered unexpected error in runtime:\n");
304-
jl_static_show(JL_STDERR, jl_current_exception());
305-
jl_printf(JL_STDERR, "\n");
303+
jl_printf((JL_STREAM*)STDERR_FILENO, "Internal error: encountered unexpected error in runtime:\n");
304+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
305+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
306306
jlbacktrace(); // written to STDERR_FILENO
307307
src = NULL;
308308
}

src/init.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ JL_DLLEXPORT void jl_atexit_hook(int exitcode)
223223
ptls->world_age = last_age;
224224
}
225225
JL_CATCH {
226-
jl_printf(JL_STDERR, "\natexit hook threw an error: ");
227-
jl_static_show(JL_STDERR, jl_current_exception());
226+
jl_printf((JL_STREAM*)STDERR_FILENO, "\natexit hook threw an error: ");
227+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
228+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
229+
jlbacktrace(); // written to STDERR_FILENO
228230
}
229231
}
230232
}
@@ -258,8 +260,10 @@ JL_DLLEXPORT void jl_atexit_hook(int exitcode)
258260
//error handling -- continue cleanup, as much as possible
259261
assert(item);
260262
uv_unref(item->h);
261-
jl_printf(JL_STDERR, "error during exit cleanup: close: ");
262-
jl_static_show(JL_STDERR, jl_current_exception());
263+
jl_printf((JL_STREAM*)STDERR_FILENO, "error during exit cleanup: close: ");
264+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
265+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
266+
jlbacktrace(); // written to STDERR_FILENO
263267
item = next_shutdown_queue_item(item);
264268
}
265269
}

src/jlapi.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ static int exec_program(char *program)
492492
jl_load(jl_main_module, program);
493493
}
494494
JL_CATCH {
495+
// TODO: It is possible for this output
496+
// to be mangled due to `jlbacktrace`
497+
// printing directly to STDERR_FILENO.
495498
jl_value_t *errs = jl_stderr_obj();
496499
JL_GC_PUSH1(&errs);
497500
volatile int shown_err = 0;
@@ -510,11 +513,11 @@ static int exec_program(char *program)
510513
}
511514
JL_GC_POP();
512515
if (!shown_err) {
513-
jl_static_show(JL_STDERR, jl_current_exception());
514-
jl_printf(JL_STDERR, "\n");
516+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
517+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
515518
}
516-
jlbacktrace();
517-
jl_printf(JL_STDERR, "\n");
519+
jlbacktrace(); // written to STDERR_FILENO
520+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
518521
return 1;
519522
}
520523
return 0;
@@ -597,10 +600,10 @@ static NOINLINE int true_main(int argc, char *argv[])
597600
free(line);
598601
line = NULL;
599602
}
600-
jl_printf(JL_STDERR, "\nparser error:\n");
601-
jl_static_show(JL_STDERR, jl_current_exception());
602-
jl_printf(JL_STDERR, "\n");
603-
jlbacktrace();
603+
jl_printf((JL_STREAM*)STDERR_FILENO, "\nparser error:\n");
604+
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
605+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
606+
jlbacktrace(); // written to STDERR_FILENO
604607
}
605608
}
606609
return 0;

src/task.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ JL_DLLEXPORT void jl_switchto(jl_task_t **pt)
558558

559559
JL_DLLEXPORT JL_NORETURN void jl_no_exc_handler(jl_value_t *e)
560560
{
561-
jl_printf(JL_STDERR, "fatal: error thrown and no exception handler available.\n");
562-
jl_static_show(JL_STDERR, e);
563-
jl_printf(JL_STDERR, "\n");
564-
jlbacktrace();
561+
jl_printf((JL_STREAM*)STDERR_FILENO, "fatal: error thrown and no exception handler available.\n");
562+
jl_static_show((JL_STREAM*)STDERR_FILENO, e);
563+
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
564+
jlbacktrace(); // written to STDERR_FILENO
565565
jl_exit(1);
566566
}
567567

test/channels.jl

+3-5
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,9 @@ end
353353
@test istaskdone(t)
354354
@test fetch(t)
355355
@test run[] == 3
356-
@test fetch(errstream) == """
357-
error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
358-
error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
359-
error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
360-
"""
356+
output = fetch(errstream)
357+
@test 3 == length(findall(
358+
"""error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")""", output))
361359
# test for invalid state in Workqueue during yield
362360
t = @async nothing
363361
t._state = 66

0 commit comments

Comments
 (0)