#542 Add first draft for chronos-multithreading docs#543
#542 Add first draft for chronos-multithreading docs#543PhilippMDoerner wants to merge 1 commit into
Conversation
| proc cleanupThread*() {.gcsafe, raises: [].}= | ||
| {.cast(gcsafe).}: | ||
| try: | ||
| `=destroy`(getThreadDispatcher()) |
There was a problem hiding this comment.
Finally had time to look into this problem a bit more - while this trick releases the (ORC) memory held by the dispatcher, the bigger issue is that the other resources (such as the IOCP/selector) still cause a leak per thread (since one selector is allocated per thread).
Instead of this partial solution, I think that the documentation would do better to document the status quo:
"On thread exit, some thread-local resources used by chronos are not released - it is recommended to use a thread pool and reuse chronos threads for this reason. For more information, see issue https:/// ..."
There was a problem hiding this comment.
I haven't written nim in a hot minute (basically since the PR), so I'm going to be a bit rusty and not up to date with the latest and greatest changes to nim.
Though I'll assume the memory management situation for individual threads basically didn't change, is that assumption correct?
If I understand you correctly, given with how difficult it is to catch all instances of thread-local memory usage and free it in time, the better solution is to basically avoid any approach where threads get spawned and die regularly?
There was a problem hiding this comment.
the better solution is to basically avoid any approach where threads get spawned and die regularly?
Yes, this is the case both for chronos specifically and for nim in general - unless you use -d:useMalloc, nim does not return memory to the OS so any memory used by a dead thread will remain "reserved" - at least, this was the case last I checked - documenting this point should link to upstream as well.
That said, we're also investigating options for performing a graceful shutdown.
First draft for what I learned with chronos in multithreading so far and how that could be turned into docs as discussed in #542 .
If you had a different vision @arnetheduck let me know, I'll see what I can do.
Note that I think a rather obvious question that I do not yet know the answer for is:
"What do I do when I want the thread to end, but the Dispatcher still has work on it? How do I tell it to process the remaining work?"
If I had an answer to that I'd gladly add an example to the docs.