Flush stdout and stderr during hs_exit() (#5594)
authorSimon Marlow <marlowsd@gmail.com>
Mon, 7 Nov 2011 15:24:00 +0000 (15:24 +0000)
committerSimon Marlow <marlowsd@gmail.com>
Tue, 8 Nov 2011 13:25:33 +0000 (13:25 +0000)
Ensures that these handles are flushed even when the RTS is being used
as a library, with no main.

Needs a corresponding change to libraries/base.

rts/Prelude.h
rts/RtsStartup.c

index 86bb609..c103b1e 100644 (file)
@@ -47,6 +47,8 @@ PRELUDE_CLOSURE(base_GHCziConcziSync_runSparks_closure);
 PRELUDE_CLOSURE(base_GHCziConcziIO_ensureIOManagerIsRunning_closure);
 PRELUDE_CLOSURE(base_GHCziConcziSignal_runHandlers_closure);
 
+PRELUDE_CLOSURE(base_GHCziTopHandler_flushStdHandles_closure);
+
 PRELUDE_INFO(ghczmprim_GHCziTypes_Czh_static_info);
 PRELUDE_INFO(ghczmprim_GHCziTypes_Izh_static_info);
 PRELUDE_INFO(ghczmprim_GHCziTypes_Fzh_static_info);
index dc62143..6e803bb 100644 (file)
@@ -69,6 +69,8 @@ void exitLinker( void );      // there is no Linker.h file to include
 // Count of how many outstanding hs_init()s there have been.
 static int hs_init_count = 0;
 
+static void flushStdHandles(void);
+
 /* -----------------------------------------------------------------------------
    Initialise floating point unit on x86 (currently disabled; See Note
    [x86 Floating point precision] in compiler/nativeGen/X86/Instr.hs)
@@ -296,6 +298,8 @@ hs_exit_(rtsBool wait_foreign)
     
     OnExitHook();
 
+    flushStdHandles();
+
     // sanity check
 #if defined(DEBUG)
     checkFPUStack();
@@ -407,6 +411,17 @@ hs_exit_(rtsBool wait_foreign)
     freeRtsArgs();
 }
 
+// Flush stdout and stderr.  We do this during shutdown so that it
+// happens even when the RTS is being used as a library, without a
+// main (#5594)
+static void flushStdHandles(void)
+{
+    Capability *cap;
+    cap = rts_lock();
+    rts_evalIO(cap, &base_GHCziTopHandler_flushStdHandles_closure, NULL);
+    rts_unlock(cap);
+}
+
 // The real hs_exit():
 void
 hs_exit(void)