CStub finalisers are never run
While working on #27036, I noticed that we don't have the equivalent of `emitInitializerDecls :: Module -> ForeignStubs -> CgStream RawCmmGroup ()` for finalizers.
This led me to believe, we might not actually run the finalizers. However, this is really tricky to test, as most `ForeignStubs` don't have finalizers... Luckily, `static` does have a finalizer!
I modified GHC to print out debug messages for the functions that should be called by the initialiser and finalizer:
```diff
diff --git a/rts/StaticPtrTable.c b/rts/StaticPtrTable.c
index b3db8f72ff9..e322065b78f 100644
--- a/rts/StaticPtrTable.c
+++ b/rts/StaticPtrTable.c
@@ -13,6 +13,7 @@
#include "RtsUtils.h"
#include "Hash.h"
#include "StablePtr.h"
+#include "rts/Messages.h"
static HashTable * spt = NULL;
@@ -50,6 +51,7 @@ void hs_spt_insert_stableptr(StgWord64 key[2], StgStablePtr *entry) {
}
void hs_spt_insert(StgWord64 key[2], void *spe_closure) {
+ debugBelch("### hs_spt_insert\n");
// Cannot remove this indirection yet because getStablePtr()
// might return NULL, in which case hs_spt_lookup() returns NULL
// instead of the actual closure pointer.
@@ -66,12 +68,12 @@ static void freeSptEntry(void* entry) {
}
void hs_spt_remove(StgWord64 key[2]) {
+ debugBelch("### hs_spt_remove\n");
if (spt) {
ACQUIRE_LOCK(&spt_lock);
StgStablePtr* entry = removeHashTable_(spt, (StgWord)key, NULL,
hashFingerprint, compareFingerprint);
RELEASE_LOCK(&spt_lock);
if (entry)
freeSptEntry(entry);
}
```
This trivial test program:
```haskell
{-# LANGUAGE StaticPointers #-}
{-# LANGUAGE ScopedTypeVariables #-}
import GHC.StaticPtr
-- A simple function we want to reference statically
hello :: String -> String
hello name = "Hello, " ++ name ++ "!"
-- Create a static pointer to the function
staticHello :: StaticPtr (String -> String)
staticHello = static hello
-- Create a static pointer to the function
staticHello2 :: StaticPtr (String -> String)
staticHello2 = static hello
-- Create a static pointer to the function
staticHello3 :: StaticPtr (String -> String)
staticHello3 = static hello
main :: IO ()
main = do
-- Dereference and use it
let f = deRefStaticPtr staticHello
putStrLn $ f "World"
-- Dereference and use it
let f = deRefStaticPtr staticHello2
putStrLn $ f "World"
-- Dereference and use it
let f = deRefStaticPtr staticHello3
putStrLn $ f "World"
-- You can also get the key (useful for serialization)
let key = staticKey staticHello
putStrLn $ "Static key: " ++ show key
-- You can also get the key (useful for serialization)
let key = staticKey staticHello2
putStrLn $ "Static key: " ++ show key
-- You can also get the key (useful for serialization)
let key = staticKey staticHello3
putStrLn $ "Static key: " ++ show key
```
And then:
```bash
$ ../ghc-hpc-bc/_build/stage1/bin/ghc Static.hs -o Static -rtsopts -debug
$ ./Static +RTS -Ds
### hs_spt_insert
### hs_spt_insert
### hs_spt_insert
created capset 0 of type 2
created capset 1 of type 3
cap 0: initialised
assigned cap 0 to capset 0
assigned cap 0 to capset 1
new task (taskCount: 1)
cap 0: created thread 1[""]
new bound thread (1)
cap 0: schedule()
cap 0: running thread 1[""] (ThreadRunGHC)
Hello, World!
Hello, World!
Hello, World!
Static key: d2b6601c4ebf866512a89634c311d9d7
Static key: 11b6da5b55fbfa3f9928aa564d2d2b6a
Static key: 3e1241819ab6d1a7f6669a9e78ccf059
cap 0: thread 1[""] stopped (finished)
bound thread (1) finished
task exiting
cap 0: created thread 2[""]
new bound thread (2)
cap 0: schedule()
cap 0: running thread 2[""] (ThreadRunGHC)
cap 0: thread 2[""] stopped (finished)
bound thread (2) finished
task exiting
all threads:
threads on capability 0:
other threads:
deleting all threads
cap 0: starting GC
cap 0: GC working
cap 0: GC idle
cap 0: GC done
cap 0: GC idle
cap 0: GC done
cap 0: GC idle
cap 0: GC done
cap 0: Memory Return (Current: 6) (Needed: 8) (Returned: 0)
cap 0: all caps stopped for GC
cap 0: finished GC
running C finalizers, 1 remaining
ran 1 C finalizers
task exiting
freeing task manager, 0 tasks still running
removed cap 0 from capset 0
removed cap 0 from capset 1
cap 0: shutting down
deleted capset 0
deleted capset 1
```
As we can see, the initialisers debug messages in the first three lines are executed, but the finalizers (i.e. `### hs_spt_remove`) are not printed.
Proof that we have finalizers:
```haskell
sptModuleInitCode :: Platform -> Module -> [SptEntry] -> CStub
sptModuleInitCode platform this_mod entries
...
| otherwise =
initializerCStub platform init_fn_nm empty init_fn_body `mappend`
finalizerCStub platform fini_fn_nm empty fini_fn_body -- <- This here is the finalizer that should call `hs_spt_remove` on shutdown
where
...
fini_fn_body = vcat
[ text "StgWord64 k" <> int i <> text "[2] = "
<> pprFingerprint fp <> semi
$$ text "hs_spt_remove" <> parens (char 'k' <> int i) <> semi
| (i, (SptEntry _ fp)) <- zip [0..] entries
]
```
```
GHC 18fd0df60d827ec1c4c84aad2283570e7635222b
issue