gleam-lang / language-tour

👩🏽‍💻 Learn Gleam in your browser
https://tour.gleam.run/
82 stars 62 forks source link

Intermittent failure to get/show output #17

Closed TechnoTone closed 8 months ago

TechnoTone commented 8 months ago

Intermittently, upon clicking 'next', I don't get any results in the output window.

Clicking 'back' & 'next' it then loads and shows the output as expected.

Alternatively, refreshing the page will also show the output, as expected.

I tested with incognito windows, and the problem still occurs.

I cannot look for any console or network errors, as, for some reason I haven't figured out yet, it never shows any output when I open the dev tools.

The problem was confirmed by another user on the Gleam discord, who also reported seeing the following error message in their browser console:

tour.gleam.run/:1 Unchecked runtime.lastError: The message port closed before a response was received.

TechnoTone commented 8 months ago

(I am using Arc browser.)

Nicd commented 8 months ago

(I'm the other user with this intermittent issue.)

The mentioned error "Unchecked runtime" seems likely to be due to extensions: https://stackoverflow.com/questions/54126343/how-to-fix-unchecked-runtime-lasterror-the-message-port-closed-before-a-respon

I'm using Vivaldi and I tested in a private window, but that window still runs Vivaldi's tracker & ad blocker, and I guess possibly some other internal Vivaldi extensions.

Upon re-entering the site (in a new private window), there is no issue, and I couldn't repeat it reliably. So it may be that this log is just a red herring.

I just opened the site on Chrome 120 and this happened. When I open devtools, there is nothing in the console. This Chrome has no extensions installed. So it can happen without extensions on a major browser, but I could not reproduce it by moving on the site or by refreshing.

DanielSherlock commented 8 months ago

I had it happen to me sometimes on Chromium 120 with no extensions. Can't say I've seen anything like the "unchecked runtime" error.

For me, just adding some meaningless whitespace somewhere in the editor has always been enough to trigger a compilation.

DanielSherlock commented 8 months ago

Okay, I was curious, so I looked at the code that calls the web worker. I've not used web workers before but it turns out they've got a fairly simple API.

There's some bits of code that clearly need tidying up (e.g. workerWorking is sometimes-but-not-always checked when sending the worker more work - even though it's never once set to true). But I don't think that was the issue.

From experimenting a bit, I reckon it's a bit of a race condition caused by worker.js having top-level await. Meaning the worker is still awaiting the gleam compiler and not yet set up to receive messages from the page... when the page sends the first message. And for some reason, I think having devtools open triggers this race condition reliably - I don't know?

Anyway, I have an idea how to solve this, so I'll muck around in devtools a bit longer hoping I get something working, then figure out how to merge that if I do.

DanielSherlock commented 8 months ago

It works (at least, with devtools open)! And as a bonus I also figured out how to change the behaviour, of the worker so that now when I have invalid code I don't see the io.println/io.debug output from the previous valid code state. (I'll make a separate issue for this though).

lpil commented 8 months ago

I believe this to be fixed. Let me know if you encounter this again.