diff --git a/src/racket/src/env.c b/src/racket/src/env.c index d086d1c4c1..ff6a543fc8 100644 --- a/src/racket/src/env.c +++ b/src/racket/src/env.c @@ -511,6 +511,10 @@ Scheme_Env *scheme_place_instance_init(void *stack_base) { } void scheme_place_instance_destroy() { + /* shutdown custodian */ + /* run atexit handlers to flush file ports */ + scheme_run_atexit_closers(); + scheme_end_futures_per_place(); #if defined(MZ_USE_PLACES) scheme_kill_green_thread_timer(); diff --git a/src/racket/src/port.c b/src/racket/src/port.c index f4c5d5e4de..2090ef8d08 100644 --- a/src/racket/src/port.c +++ b/src/racket/src/port.c @@ -556,15 +556,6 @@ scheme_init_port (Scheme_Env *env) } #endif -#ifdef MZ_FDS - scheme_add_atexit_closer(flush_if_output_fds); - /* Note: other threads might continue to write even after - the flush completes, but that's the threads' problem. - All writing by the main thread will get flushed on exit - (but not, of course, if the thread is shutdown via a - custodian). */ -#endif - register_port_wait(); scheme_add_global_constant("subprocess", scheme_make_prim_w_arity2(subprocess, "subprocess", 4, -1, 4, 4), env); @@ -588,6 +579,16 @@ scheme_init_port (Scheme_Env *env) void scheme_init_port_places(void) { + +#ifdef MZ_FDS + scheme_add_atexit_closer(flush_if_output_fds); + /* Note: other threads might continue to write even after + the flush completes, but that's the threads' problem. + All writing by the main thread will get flushed on exit + (but not, of course, if the thread is shutdown via a + custodian). */ +#endif + REGISTER_SO(read_string_byte_buffer); REGISTER_SO(scheme_orig_stdout_port); REGISTER_SO(scheme_orig_stderr_port); diff --git a/src/racket/src/schpriv.h b/src/racket/src/schpriv.h index f4119f7497..33762feb38 100644 --- a/src/racket/src/schpriv.h +++ b/src/racket/src/schpriv.h @@ -558,6 +558,7 @@ typedef struct Scheme_Custodian_Box { Scheme_Thread *scheme_do_close_managed(Scheme_Custodian *m, Scheme_Exit_Closer_Func f); Scheme_Custodian *scheme_get_current_custodian(void); +void scheme_run_atexit_closers(void); typedef struct Scheme_Security_Guard { Scheme_Object so; diff --git a/src/racket/src/thread.c b/src/racket/src/thread.c index c1d6d40171..1a5916656d 100644 --- a/src/racket/src/thread.c +++ b/src/racket/src/thread.c @@ -1692,14 +1692,16 @@ static void run_closers(Scheme_Object *o, Scheme_Close_Custodian_Client *f, void { Scheme_Object *l; - for (l = cust_closers; SCHEME_RPAIRP(l); l = SCHEME_CDR(l)) { - Scheme_Exit_Closer_Func cf; - cf = (Scheme_Exit_Closer_Func)SCHEME_CAR(l); - cf(o, f, data); + if (cust_closers) { + for (l = cust_closers; SCHEME_RPAIRP(l); l = SCHEME_CDR(l)) { + Scheme_Exit_Closer_Func cf; + cf = (Scheme_Exit_Closer_Func)SCHEME_CAR(l); + cf(o, f, data); + } } } -static void run_atexit_closers(void) +void scheme_run_atexit_closers(void) { mz_jmp_buf newbuf, *savebuf; @@ -1726,12 +1728,12 @@ void scheme_add_atexit_closer(Scheme_Exit_Closer_Func f) { if (!cust_closers) { if (replacement_at_exit) { - replacement_at_exit(run_atexit_closers); + replacement_at_exit(scheme_run_atexit_closers); } else { #ifdef USE_ON_EXIT_FOR_ATEXIT - on_exit(run_atexit_closers, NULL); + on_exit(scheme_run_atexit_closers, NULL); #else - atexit(run_atexit_closers); + atexit(scheme_run_atexit_closers); #endif }