Api and threading issue?

A new update is now available, introducing "Points of interest" and many more changes!
Latest hotfix: 0.9 (2025-11-05)
  • i have been working on a rather large plugin and i NEED to implement threading. i have been driving myself crazy with an issue tho. it seems to me that if i create a new thread the API creates hundreds of new threads. i have done some testing and i dont no think it is my code. any input would be helpful.


    Java
    // Init player method
    public void initPlayer(Player player){
    if(rpCore.getConfig().getDevConfig().getMultiThreadEnabled()){
    Thread initThread = new Thread(new InitPlayer(rpCore, player, rpCore.getConfig().getDevConfig().getMultiThreadEnabled()));
    initThread.start();
    }else{
    new InitPlayer(rpCore, player, rpCore.getConfig().getDevConfig().getMultiThreadEnabled());
    }
    }



    Java
    //ps.debug method
    println("[RPCore/PlayerSettings]" , 9);
    for (Map.Entry<String, String> entry : settings.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    println("\t "+key+": -> "+value, 14);
    }


    logs to show what i think is an issue

    Threaded threaded console output.txt

    NON-Threaded non threaded consol output.txt


    Again it may be me but o do not believe it is.

  • Same thing i se on my plugin too. I use a background thread to move a vehicle along a route. First i tried with Timer and well worked well, but limited to a single vehicle. Latter i use proper thread to move many simultaneous vehicle, and when i use my thread i see a lot of messages like:



    On java side :



    And BTW when i use Custom Event



    And when i used custom event and register this event for listening i get in log for every custom event:


    Code
    [Java] -> Event: bamse.rw.ferry.events.FerryMoveToEvent
    [ERROR] [09:08:30] Java RegisterEvent - Event not found: Events.FerryMoveToEvent
    UnityEngine.Debug:LogError(Object)
    RisingWorld.PluginAPI.PluginEventHandler:OnRegisterEvent(IntPtr, JObject, JString) (at C:/GitLab-Runner/builds/jxpAq3Km/2/jiw-games/rising-world/Assets/Scripts/PluginAPI/PluginEventHandler.cs:258)
  • Thanks for bringing this to my attention! :wat: I can confirm that this is a bug in the API: the native JNI interface is only valid in the current thread, or more precisely, each thread requires a separate JNI env pointer (this is a restriction from JNI). When accessing an API method which communicates with the native side (which is the case for most methods and objects) from another thread, the game creates a new JNIenv pointer for that thread. Unfortunately it does not cache this pointer, so a new pointer will be created for subsequent accesses. This is quite problematic and can result in a number of issues.


    I will prepare a fix for that... do you only run your plugins on dedicated servers atm?

  • i tested too only on dedicated server.


    I see this behavior some time ago and suspected is something with context switch of thread but i don't reported yet as possible problem and almost forgot about it. Thx james1bow to bring this to attention!

  • Just to avoid misunderstandings: the game does not spawn new threads or something like that. It just creates a new JNIenv pointer everytime you access an API method from a new thread (that was not created by the game). It's a waste of resources ofc and could also result in other issues, so it's definitely a more serious bug.


    Unfortunately there are no more hotfixes planned, but I'll prepare at least a hotfix for the server which fixes this bug, would that work for you? The next regular update (which also contains the fix) should still be ready this year :)

  • About error from log when i use custom events? It seems is just a wrong message for custom events. All event are fired and work correctly.


    Code
    [Java] -> Event: bamse.rw.ferry.events.FerryMoveToEvent
    [ERROR] [09:08:30] Java RegisterEvent - Event not found: Events.FerryMoveToEvent
    UnityEngine.Debug:LogError(Object)
    RisingWorld.PluginAPI.PluginEventHandler:OnRegisterEvent(IntPtr, JObject, JString) (at C:/GitLab-Runner/builds/jxpAq3Km/2/jiw-games/rising-world/Assets/Scripts/PluginAPI/PluginEventHandler.cs:258)
  • Just to avoid misunderstandings: the game does not spawn new threads or something like that. It just creates a new JNIenv pointer everytime you access an API method from a new thread (that was not created by the game). It's a waste of resources ofc and could also result in other issues, so it's definitely a more serious bug.


    Unfortunately there are no more hotfixes planned, but I'll prepare at least a hotfix for the server which fixes this bug, would that work for you? The next regular update (which also contains the fix) should still be ready this year :)

    whatever works for you. I've added a sett8ng for now to disable multi threads in my plugin so really no rush. AS long as its not really creating hundreds of threads lol.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!