Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezing upon loading savestates #32

Open
BLCRAFT210 opened this issue Feb 6, 2023 · 9 comments
Open

Freezing upon loading savestates #32

BLCRAFT210 opened this issue Feb 6, 2023 · 9 comments

Comments

@BLCRAFT210
Copy link

BLCRAFT210 commented Feb 6, 2023

I've noticed a few ways that savestate functions can cause Dolphin to freeze. The first is when you attempt to load a savestate with the scripting API that wasn't created using the API. (Edit: This actually works, but for some reason the game speed is much faster than normal upon loading.) Likewise, attempting to load a savestate with a hotkey will also cause a freeze if the savestate was created using the API. Finally, loading a savestate twice in the same program causes a freeze. This is a quick program that causes that:

from dolphin import event, savestate

await event.frameadvance()
savestate.load_from_file([path to file]) #this one works fine
for i in range(60):
    await event.frameadvance()
savestate.load_from_file([path to file]) #this one crashes

I don't know why these freezes happen, and there might be some other bugs with the savestate module.

@BLCRAFT210
Copy link
Author

BLCRAFT210 commented Feb 6, 2023

Actually, I just tested the above program using a different savestate and it worked just fine. I don't know why it works with one savestate but not the other.

@BLCRAFT210
Copy link
Author

Actually, I just tested the above program using a different savestate and it worked just fine. I don't know why it works with one savestate but not the other.

I found some potential clues to what causes these freezes. The savestate I mention here freezes on the second load when V-Sync is enabled, but it works fine again when V-Sync is disabled. I then found an easier fix which worked for all savestates I tested: setting the emulation speed limit to 10%.

I don't know much about how Dolphin or this branch works, but I have some guesses for what's going on here. I think the problem isn't with loading a savestate twice; it's with loading a savestate in the middle of emulation. It appears that somehow, giving Dolphin more time to fully load a savestate before it advances to the next frame fixes this problem. Therefore, this freeze may be caused by Dolphin trying to step to the next frame while savestate.load_from_file is in the middle of loading a savestate. Perhaps the Python function is missing something that Dolphin's savestate implementation does to prevent this problem.

@BLCRAFT210
Copy link
Author

Also, as @VIPTankz found out, adding time.sleep(1) before loading the savestate fixes this.

@JimB16
Copy link

JimB16 commented Feb 13, 2023

I use the savestate.load_from_file function a lot myself and I haven't had any big problems with it (Emulation Speed is 200% most of the times). I assume the reason is that I load my savestates while dolphin is interrupted with the frameadvance() event. The few times I seemed to have run into a problem I also assumed a timing misbehavior, but I haven't had any bigger issues that I could reproduce.

Looking at dolphins MainWindow::StateLoad() implementation of its GUI I can't see any code that would make sure that the timing is right for the SaveState loading or halt the emulation.

@BLCRAFT210
Copy link
Author

@JimB16 what's an example of a program that loads savestates properly?

@Felk
Copy link
Owner

Felk commented Feb 22, 2023

Yeah, it's possible there's a bunch of issues wrt race conditions. I'll have to make a stresstest and see if more rigorous synchronization between python code and emulation will fix it. Thanks for bringing it up, I'll leave this issue open for now.

@benjaminjmiddleton
Copy link

benjaminjmiddleton commented Mar 7, 2023

I'm also experiencing the same issue, only none of the workarounds here seem to fix it, disabling VSync, making new save states, slowing emulator speed, using time.sleep() before or after loading.

Throwing my own (much more convoluted) workaround in for anyone else with this issue:

from pynput.keyboard import Controller, Key

keyboard = Controller()

# some code

# then, replace your savestate.load_from_slot(1) with the following
keyboard.press(Key.f1)
time.sleep(0.1)
keyboard.release(Key.f1)

This workaround requires that the highlighted window is Dolphin, and requires external packages. But it's working for me, so I'm throwing it out for anyone that might have the same issue.

@BLCRAFT210
Copy link
Author

This should work even if the window isn't highlighted after checking Background Input in controller settings. I'll check if this works for me tomorrow.

@benjaminjmiddleton
Copy link

Thanks, I didn't know about that setting! I will try that when I get the chance later this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants