Repository: hedyorg/hedy

Issue:
No or incorrect output [BUG]
Sometimes when my students run a program they don't get any or incorrect output. It is hard to pinpoint when exactly this happens. I think it has to do with pressing "Run Code" while a program is still running. Student do this a lot and I see this issue multiple times per lesson. Running the program keeps on giving incorrect results once this has happened. My workaround is to save the program, refresh the webpage in the browser and try again. This usually works.

One example of this issue arises when I run the code below and press the "Run Code" button multiple times during execution. At one point it will create two figures and raise an error (see screenshot). However the issue is certainly not limited to drawings, it also happens when using the `repeat` and `ask` commands.

```
hoek = 90
repeat 10 times
    turn hoek
    forward 50
forward 100
```

![image](https://user-images.githubusercontent.com/10562903/150156604-ddb95243-c88f-4ff3-a791-db2dda773c86.png)



Discussion:
Thanks for reporting this @OnnoEbbens! I also have the feeling I have this issue sometimes (and I refresh to solve it)

@TiBiBa Can you try to see if you can reproduce and to understand this?
> Thanks for reporting this @OnnoEbbens! I also have the feeling I have this issue sometimes (and I refresh to solve it)
> 
> @TiBiBa Can you try to see if you can reproduce and to understand this?

Yes, will take a look! It does seem like a weird bug as pressing the run button should terminate all running processes. 

Yeah it is really hard to reproduce precisely, which is why I did not yet create an issue. I have the feeling it happens after you had an error, on run right after that (with our without the same error)
> Yeah it is really hard to reproduce precisely, which is why I did not yet create an issue. I have the feeling it happens after you had an error, on run right after that (with our without the same error)

Yes, I'm trying to reproduce the error but no luck so far. It might have something to-do with the front-end timeout we have. Maybe it does not reset correctly in the case of a faulty run, will dive into the code to try and find the cause.
It might have something to-do with switching browser tabs? @OnnoEbbens The timer continues to run, but for some reason the turtle pauzes when you are on another browser tab. This way I was able to easily reproduce the error:


![Schermafbeelding 2022-01-19 om 17 29 57](https://user-images.githubusercontent.com/38155424/150173381-f095b069-c725-4f97-978c-4fbbecc743c0.png)

Resolving patch:
diff --git a/website/achievements.py b/website/achievements.py
--- a/website/achievements.py
+++ b/website/achievements.py
@@ -183,8 +183,8 @@ def check_code_achievements(self, code, level):
             for command in set(commands_in_code):
                 if command not in session['commands'] and command not in session['new_commands']:
                     session['new_commands'].append(command)
-        if set(session['commands']).union(session['new_commands']) == self.all_commands:
-            session['new_achieved'].append("trying_is_key")
+            if set(session['commands']).union(session['new_commands']) == self.all_commands:
+                session['new_achieved'].append("trying_is_key")
         if 'did_you_say_please' not in session['achieved'] and "ask" in hedy.all_commands(code, level, session['lang']):
             session['new_achieved'].append("did_you_say_please")
         if 'talk-talk-talk' not in session['achieved'] and hedy.all_commands(code, level, session['lang']).count("ask") >= 5:


Test patch: