Fix worker thread memory leaks (solves error 2011-0301, threadCreate rc 559) #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The crashes happened due to a memory leak related to the sysmodule's worker threads. When exiting one of the said threads, ldn_mitm erroneously calls svcExitThread before workerThread.Join which makes the latter fail to exit cleanly and free is resources (return code 5977) and that causes the aforementioned leak. After some time, the sysmodule runs out of free memory and, thus, it becomes unable to create new worker threads (threadCreate return code 559, aka LibnxError_OutOfMemory). That's why you must reset the console once it crashes to make it work again.
Unless I misunderstood what the code does, calling svcExitThread shouldn't be needed at all because Join already waits for a thread to be done executing and then terminates it cleanly (in a nutshell, it first calls threadWaitForExit and then threadClose) so all this mod does is it just removes the svcExitThread call - so far, I haven't seen any adverse effects by doing so after around 24 hours of mixed online gameplay/idling, nor any Join/threadCreate errors in the logs. An issue like this wouldn't be limited to Pokémon only and old logs seem to confirm it, however, the crashes likely only popped up now because Pokémon sends a metric crapton of stuff over the network compared to the other Switch games, thus exhausting the available memory more quickly.
(Quoted from my post here on the temp. Other people have reported no issues too since then.)