Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Feature Request Put option to force stop threads when exit playmode editor.

Discussion in 'Editor & General Support' started by treviasxk, Aug 3, 2023.

  1. treviasxk

    treviasxk

    Joined:
    Apr 22, 2015
    Posts:
    33
    It would be very useful to have such an option in the project settings to stop all game threads when exiting play mode in the editor.

    When building the game, the threads work perfectly because they finish when the game is closed, but when testing the game in the editor, the threads continue to run even after exiting playmode, which can disrupt development.
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    If you use threads, managing them is really the developer's job, and any multi threaded application needs to implement some sort of thread shutdown. It's not really just the shutdown but threads need careful management altogether. You can use the OnDestroy() methods for destructor code, just kill them there.
     
  3. treviasxk

    treviasxk

    Joined:
    Apr 22, 2015
    Posts:
    33
    I'd rather it be a optional Unity feature to force stop threads in playmode editor, than me configuring a safe thread for each plugin/library that doesn't stop work properly.
     
    Last edited: Aug 4, 2023
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Unity is a single thread platform and Unity API is accessible only from the main thread. Your main thread is this privileged thread you're looking for, so you can kill the other threads from, say, your MonoBehaviour OnDestroy() methods because it is guaranteed they will get called on shutdown.

    Multithreading in Unity is a niche feature and requires the coder knows what he's doing.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    What threads do you refer to as „the threads“?

    How do you determine that these threads keep running, and more importantly, are you sure they aren‘t supposed to keep running in the editor? What issues do you have with these threads anyway?

    Unity just stopping all threads would be a terrible idea.
     
  6. treviasxk

    treviasxk

    Joined:
    Apr 22, 2015
    Posts:
    33
    • What threads do you refer to as „the threads“?
    Not referring to the Unity Editor threads, just the threads the game creates when running in editor playmode (test mode).

    How do you determine that these threads keep running?
    As I said in the topic, I want you to add an option in Unity (a checkbox) so that when I check the threads created by the game, they will be finalized after I leave the playmode editor. (this is the same thing if I open the release game executable and close the game, automatically the threads are forced to stop, but in playmode editor it is different)

    • And more importantly, are you sure they aren't supposed to keep running in the editor?

    Yes, I'm using a mysql plugin to connect to a database, it creates a thread when you connect to the server, and this connection (thread) doesn't stop when you call it in the script.
    If you research about it, everyone recommends using the using() command because it destroys a thread when the script ends, but the problem in this case does not using only server to make a single connection, in my case I am creating a server, the connection with mysql needs to be connected until the end of the program, you can't stop this thread because the command MySqlConnection.Close(); It does not work properly.

    • Unity just stopping all threads would be a terrible idea.
    I'm talking about stopping only the threads generated by the game in playmode Editor. There is no problem with someone forcing these threads to stop, this is the same thing as closing or terminating any program through the task manager, you will always be able to open it and if it crashes I guarantee you that it was not because the threads were stopped, but because it was badly programmed.
     
  7. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    If you have a persistent database connection, close the connection manually upon shutdown. Business as usual for any application.
     
    CodeSmile likes this.
  8. treviasxk

    treviasxk

    Joined:
    Apr 22, 2015
    Posts:
    33
    I'd rather have all game threads behave like a release build than waste time manually disconnecting the connection or looking for workarounds for any plugins/libraries I add. That's why I'm asking for this option.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,498
    Terminating threads not at process shutdown is a great way to get yourself in a deadlock. If you happen to terminate a thread that holds a lock, then the next time somebody tries to take that lock, you will hang forever.

    Unity does request threads to shut down on domain reload. However, that only works if the thread is cooperative and not straight up ignoring the requests.

    Bonus reading: https://devblogs.microsoft.com/oldnewthing/20150814-00/?p=91811
     
  10. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I think you should investigate this issue first and foremost. If the recommendation is to enclose the connection object in a using statement it means the class implements IDisposable and when you don't use a using statement you have to manually call Dispose() on that object.

    If it's not working properly, chances are you're not using it properly. ;)
     
  11. treviasxk

    treviasxk

    Joined:
    Apr 22, 2015
    Posts:
    33
    Friend, the only thing I want with this topic of mine is to ask the unity team to add an option to stop threads, I am repeating this several times. I didn't create this topic to keep discussing about the difficulty I had with mysql, I just spoke about the mysql plugin as an example, of the difficulty I have.
    my code with mysql is working, in another topic I even put the script and marked the topic as solved.
    repeating once again, this topic of mine is just to ask for an option [Feature Request], which will be useful for several things, not just mysql.
     
  12. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    It's obviously not solved if MySQL still has an open thread running that does not get released when exiting playmode. This isn't something that's supposed to happen, and it's not Unity's responsibility to fix that.

    There is some sort of cleanup not happening with the MySQL connection otherwise you wouldn't have MySQL block the exit playmode process. The docs even state that Close() with connection pooling enabled will only return the connection to the pool, not discard it. The SO post also mentions to disable pooling for that reason. Have you looked into that?
     
  13. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    I think any multithreaded stuff falls out of scope for Unity. As a developer you can put yourself into all sorts of mess with any and all multithreaded stuff, not just if you don't know how to close database connections, but mostly if you don't know how to manage threads.

    So if Unity started catering for your needs they wouldn't need to stop at what you're suggesting. I also think it's a bad idea to let you start threads and Unity would then have to clean up after you, and it's much better that you study those things for your own sake, because one day you might be programming for another platform, too.

    I was once in a backend project where I was asked to check the code from a contractor who had been building a database connector for a month and it didn't seem to get finished despite his promises but was just taking longer and longer. I read the code and it contained stuff like catch (NullpointerException) blocks and it was obvious that if he is doing stuff like this in the code, this will never work in a reliable way but he is just trying to hide errors and get it somehow to look like working. Simply he didn't know how to manage those connections, and I wasn't blaming him, cos this is a topic that's known to be difficult cos the various different middleware and database servers each are unique and have their own quirks. He just should have asked for help instead of trying to give promises that it's soon working, if he doesn't know how to get it working.

    Moral of the story: db connections are not just plug and play but they require to be used gracefully according to best practices with your db server and your client middleware (in this case, your plugin). All of these are specific choices, and can't be fixed by some Unity master-kill-switch.
     
    Last edited: Aug 5, 2023
  14. treviasxk

    treviasxk

    Joined:
    Apr 22, 2015
    Posts:
    33
    The reason I asked for this feature is not to let these flaws in the code go unnoticed and not because I'm too lazy to stop a thread.

    Actually, it's to debug and find the problem easier, how long do you think it took me to discover that the problem of my Unity getting stuck Reloading Domain was because mysql was in a thread?, if this option existed in Unity, I would just activate it , if it didn't get more stuck in Reloading Domain it's because it has an active thread, that easy to debug.

     
  15. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    As was noted earlier, Unity does request threads to shut down at Domain Reload. But you are asking for "force stop" which means eg that Unity should delete your database connection in the middle of transfer and corrupt your DB. It's like asking Unity to also get rid of your NullPointerExceptions cos they cause you problems.