How to debug multi-threading problems
FatFS has some requirements as far as re-entrant behaviour goes (eg: FF_FS_REENTRANT) and constraints about what exactly can be called concurrently.
Also from memory the HAL USB stuff has requirements for being called concurrently.
Debugging concurrent stuff is not trivial - for example you write “after several times running this thread” - does that mean you create and destroy a new thread each time? Or that the thread is blocked, waiting for your message?
The former possibly leads to memory issues, the latter is how I’d do it personally.
You write “the program stops running” - does it crash or is your thread just blocked for some reason?