#Pure Java coroutines causing server thread to freeze.

31 messages · Page 1 of 1 (latest)

quartz terrace
#

(Moved from #mod-dev-1)
I'm trying to use this pure Java coroutine implementation to add concurrecy to a custom pathfinder, but all it does is it freezes the server thread, anyone encountered this problem and (possibly) solved it?
How I'm running the coroutine :

CoroutineScope.launch(scope -> this.findPathCoroutine.runAsync(scope,new Pair<>(navigator,request)).await());

The coroutine itself :

private final Coroutine<Pair<PathNodeNavigator,PathRequest>,Void> findPathCoroutine = first(
        apply((pair) -> {
            pair.getLeft().findPath(pair.getRight(),this::finishedProcessing);
            return null;
        })
);

Reason why I'm not using threads is because they are kinda slow for some reason, and reason why I'm not using virtual threads is because the mod is for 1.20.1 and I'm using java 17 and I don't want to lock my mod to a specific Java version.

#

original message here #mod-dev-1 message

#

moved because it's more involved and it got quickly drowned in the wave of other issues

worldly lava
quartz terrace
#

and setting the startPos and targetPos of the navigator

worldly lava
#

No, ...

quartz terrace
#

but thats synchronized

worldly lava
#

Like what does findPath do

quartz terrace
#

oh yeah, it has to access the navigator's grid

#

i use a ConcurrentHashMap to store the path nodes

worldly lava
#

So you don't touch the world, entity or server at any point after you leave the thread?

#

Until you rejoin the thread?

quartz terrace
#

i have to check if a block is solid

#

and getNode also has to create a node

worldly lava
#

Then that needs to be on thread

#

You can queue it with MinecraftServer#execute

quartz terrace
#

would that work?

worldly lava
#

But you'll need to have the result to be stored in some kind of concurrent object, and wait for the server to get around to processing it

worldly lava
quartz terrace
#

i'll mess around with it and see how it goes

#

thanks

worldly lava
#

I'm not at all familiar with this coroutine implementation. I suspect that somewhere along the way, there may be a blocking something that's getting called

#

As well

quartz terrace
#

the thing is, when i used normal threads, it "worked"

#

"worked" because the path wasnt being found and the server was working fine with no errors

#

but now i know why it didn't "work"

worldly lava
#

Yeah, Minecraft is not at all thread safe

quartz totem
#

Coroutines are not a substitute for (virtual) threads. There is no parallell execution. I recommend reading up on what coroutines are.