The problem is also that it’s very difficult to know what is being done. I had no idea that this would “break” anything, because I no idea that people sent JS objects between rules, and frankly, even if they did, I thought that the JS scripting lock would prevent GraalJS from complaining (but I realize now that the lock doesn’t cross rule boundaries, so it won’t).
But, it has been in the snapshots for a while, and there has been no complaints about the problem before the last milestones/release candidates. There might still be something I’m missing here, it’s still not entirely clear to me exactly what the “rules” are for GraalJS.
I think it’s too early to start any massive transition, there might still be things we can do that will solve most issues transparently for the users.
edit: Also, from what I understand, this also applies to JS strings..? That is a really strange and unexpected complication if that’s the case, because in Java, strings are immutable and can be shared across threads without problems. If the JS string created by GraalJS isn’t immutable and does trigger the “multi threaded access” situation, you have a situation that is much more cumbersome to handle than what it would be in Java.
Ultimately, JS, Ruby and Python all face the same challenge: These are single threaded languages that are being used in a multithreaded environment. Since they are “natively single threaded”, they don’t have the built-in tools to handle multithreading. At the same time, an application like OH would never have worked without multithreading, in addition to being much slower, any one bug that makes something hang would force “everything” to hang. A rule using sleep() would make the whole system sleep, not just the rule.
It’s not entirely clear to me what the philosophy is around threading and these scripting languages, from the makers. It looks like they basically just leave it to the “consumers” of the scripting languages to figure out. GraalJS has probably introduced the “hard fail” when multiple threads access the same object because without it, there was too much mayhem with people that think in “one dimension”/single threaded started wreaking havoc in a multithreaded environment. People will naturally blame GraalJS for being “buggy”, and trying to explain what is really going on won’t work. By implementing the “rule” the way they have done now, they make avoid the “blame” for this, but they also prevent a lot of situations that would most likely have had no ill effects (despite not being 100% “safe”) from working.
Perhaps OHs whole approach to these single-threaded languages are wrong. Perhaps we shouldn’t use the “rule threads” for executing anything in these languages, perhaps instead there should be one “GraalJS thread” that executed everything GraalJS. It would come with some caveats like worse performance and the potential for one “bad rule” to make everything else stop, but it would eliminate the possibility of ever seeing the “multithreaded access” exception, and it would allow getting rid of the complicated locking system.
For this to “work” at all in JS, it has the event loop, promises and all that. It is all made so to basically make it impossible for code to be blocking. You can’t pause execution of JS, you must create a timer or a promise that is executed at a later time instead. This is to prevent the situation where one “bad” piece of code can lock up the single thread. But, here, this isn’t a viable approach, because you can call Java functions that are blocking. Which undermines the whole “workaround” that is designed to prevent single-threaded execution from bringing everything to a halt.
So, I see this as a difficult situation no matter how you twist it.