Skip to content

#542 Add first draft for chronos-multithreading docs#543

Open
PhilippMDoerner wants to merge 1 commit into
status-im:masterfrom
PhilippMDoerner:#542-multithreading-docs
Open

#542 Add first draft for chronos-multithreading docs#543
PhilippMDoerner wants to merge 1 commit into
status-im:masterfrom
PhilippMDoerner:#542-multithreading-docs

Conversation

@PhilippMDoerner
Copy link
Copy Markdown

@PhilippMDoerner PhilippMDoerner commented May 21, 2024

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.

proc cleanupThread*() {.gcsafe, raises: [].}=
{.cast(gcsafe).}:
try:
`=destroy`(getThreadDispatcher())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:/// ..."

Copy link
Copy Markdown
Author

@PhilippMDoerner PhilippMDoerner Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants