Opened 2 years ago

Closed 2 years ago

#14725 closed bug (fixed)

memory leak: forkOS not releasing thread local `Task` struct upon end of thread

Reported by: RobertZabel Owned by:
Priority: normal Milestone: 8.4.1
Component: Runtime System Version: 8.2.2
Keywords: forkOS memory leak Cc: alexander.vershilov@…, costin.ioana@…, erick@…
Operating System: POSIX Architecture: Unknown/Multiple
Type of failure: Other Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D4346
Wiki Page:

Description (last modified by RobertZabel)

I came to notice a memory leak in the runtime system while letting warp use forkOS and conducting a load test.

A minimal snippet to reproduce:

module Main where
import Control.Concurrent
import Control.Monad

main :: IO ()
main = replicateM_ 10000000 $ forkOS $ return ()

This patch will clean up the thread local Task struct just before threads created by forkOS terminate.

diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c
index c9adf4e..bd4e75d 100644
--- a/rts/posix/OSThreads.c
+++ b/rts/posix/OSThreads.c
@@ -223,6 +223,7 @@ forkOS_createThreadWrapper ( void * entry )
     cap = rts_lock();
     rts_evalStableIO(&cap, (HsStablePtr) entry, NULL);
     rts_unlock(cap);
+    rts_done();
     return NULL;
 }

I think win32 platforms need an equivalent patch, but unfortunately I cannot verify that.
Hence I'm only targeting posix here.

Change History (5)

comment:1 Changed 2 years ago by RobertZabel

Description: modified (diff)

comment:2 Changed 2 years ago by bgamari

Differential Rev(s): Phab:D4346
Status: newpatch

Good catch and thanks for the patch! I've pushed the patch to Phab:D4346 for review.

comment:3 Changed 2 years ago by erick

Cc: erick@… added

comment:4 Changed 2 years ago by Ben Gamari <ben@…>

In 382c12d1/ghc:

rts: Ensure that forkOS releases Task on termination

Test Plan: validate

Reviewers: simonmar, erikd

Reviewed By: simonmar

Subscribers: rwbarton, thomie, carter

GHC Trac Issues: #14725

Differential Revision: https://phabricator.haskell.org/D4346

comment:5 Changed 2 years ago by bgamari

Milestone: 8.4.1
Resolution: fixed
Status: patchclosed

Fixed and merged to 8.4.

Note: See TracTickets for help on using tickets.