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

3DS Encore save state failed with Bizhawk API #3875

Closed
jlefebure opened this issue Mar 23, 2024 · 8 comments
Closed

3DS Encore save state failed with Bizhawk API #3875

jlefebure opened this issue Mar 23, 2024 · 8 comments
Labels
Core: Future core Core doesn't exist yet or is an early WIP re: APIHawk Relating to EmuHawk's public .NET API or to the creation of external tools Repro: Affects 2.9.2 dev Working as intended (wontfix) Intended behaviour perceived as a bug, or an unreasonable feature request

Comments

@jlefebure
Copy link

jlefebure commented Mar 23, 2024

Summary

Exception when saving state via SaveState API with a c# external tool

Repro

  1. Calling EmuClient.SaveState while loading a 3DS game
  2. Bizhawk crash. Exception is logged in IDE
    public ApiContainer? _maybeAPIContainer { get; set; }

    private ApiContainer APIs
        => _maybeAPIContainer!;
        //...

	 APIs.EmuClient.Pause();
        APIs.EmuClient.SaveState(SAVE_PATH + @"\"+ SAVE_PREFIX + "-" + DateTime.Now.ToString("yyyyMMddHHmm"));
        APIs.EmuClient.SaveRam();
        APIs.EmuClient.Unpause();

Output

System.Exception: Failed to set context to current! SDL error: wglMakeCurrent(): La ressource demandée est en cours d’utilisation.
   à BizHawk.Bizware.Graphics.SDL2OpenGLContext.MakeContextCurrent() dans /src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs:ligne 174
   à BizHawk.Client.EmuHawk.OpenGLProvider.ActivateGLContext(Object context) dans /src/BizHawk.Client.EmuHawk/GraphicsImplementations/OpenGLProvider.cs:ligne 23
   à BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS.Encore.ActivateGLContextCallback(IntPtr context) dans /src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.cs:ligne 190
   à Bizhawk.BizInvokeProxyLibEncore.Encore_StartSaveState(IntPtr )
   à BizHawk.Emulation.Cores.Consoles.Nintendo.N3DS.Encore.SaveStateBinary(BinaryWriter writer) dans /src/BizHawk.Emulation.Cores/Consoles/Nintendo/3DS/Encore.IStatable.cs:ligne 16
   à BizHawk.Client.Common.SavestateFile.<Create>b__6_4(BinaryWriter bw) dans /src/BizHawk.Client.Common/savestates/SavestateFile.cs:ligne 58
   à BizHawk.Client.Common.ZipStateSaver.<>c__DisplayClass6_0.<PutLump>b__0(Stream s) dans /src/BizHawk.Client.Common/savestates/ZipStateSaver.cs:ligne 46
   à BizHawk.Client.Common.FrameworkZipWriter.WriteItem(String name, Action`1 callback, Boolean zstdCompress) dans /src/BizHawk.Client.Common/FrameworkZipWriter.cs:ligne 40
   à BizHawk.Client.Common.ZipStateSaver.PutLump(BinaryStateLump lump, Action`1 callback, Boolean zstdCompress) dans /src/BizHawk.Client.Common/savestates/ZipStateSaver.cs:ligne 38
   à BizHawk.Client.Common.ZipStateSaver.PutLump(BinaryStateLump lump, Action`1 callback) dans /src/BizHawk.Client.Common/savestates/ZipStateSaver.cs:ligne 48
   à BizHawk.Client.Common.SavestateFile.Create(String filename, SaveStateConfig config) dans /src/BizHawk.Client.Common/savestates/SavestateFile.cs:ligne 60
   à BizHawk.Client.EmuHawk.MainForm.SaveState(String path, String userFriendlyStateName, Boolean fromLua, Boolean suppressOSD) dans /src/BizHawk.Client.EmuHawk/MainForm.cs:ligne 4327
   à BizHawk.Client.Common.EmuClientApi.SaveState(String name) dans /src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs:ligne 151
   à BizHawkTpp.SaveService.Save()

Host env.

  • BizHawk 2.9.2; Win10 Pro 1903; Intel/Nvidia (dev rev a803407)
@YoshiRulz YoshiRulz added re: APIHawk Relating to EmuHawk's public .NET API or to the creation of external tools Core: Future core Core doesn't exist yet or is an early WIP labels Mar 23, 2024
@YoshiRulz
Copy link
Member

Does this happen consistently?

Please copy the version from Help > About..., obviously you didn't mean to write 2.5.2.

@jlefebure
Copy link
Author

Yup, always. Save states made using the UI works.

You're right, i'm on 2.9.2, last version from master branch (rev a803407)

@CasualPokePlayer
Copy link
Member

CasualPokePlayer commented Mar 23, 2024

Is this savestate possibly occurring in a separate thread? The error seems to indicate you are in a separate thread and it's when the main thread has the Encore OpenGL context active (and therefore other threads cannot make it active). You should not be calling SaveState outside of the main thread (this is more an OpenGL limitation if anything; even then savestates APIs (along with any other API touching emulator state) should not be considered safe outside of the main thread).

@jlefebure
Copy link
Author

Hi,
Yep, i'm on a separate thread. I'm actually calling the SaveState method every 10 minutes to make an autosave. I did not encounter any issue with NDS or GBA emulation, but I assume that it does not use OpenGL

Just to be sure, there is no workaround ?

@CasualPokePlayer
Copy link
Member

CasualPokePlayer commented Mar 24, 2024

Doing savestates with other cores have very high risks of causing crashes (especially any waterbox cores like NDS due to non-thread safe code in its handling) and whatever savestates produced would very likely be garbage / not load correctly. Your code is fundamentally wrong here.

@YoshiRulz
Copy link
Member

Can't we make the call on the correct thread regardless of where the API was called from?

@CasualPokePlayer
Copy link
Member

The API user could do that with the WinForms BeginInvoke/Invoke API.

@jlefebure
Copy link
Author

Good to know. Thanks a lot @CasualPokePlayer, @YoshiRulz !

If API methods are not threadsafe, I close the issue and will work with BeginInvoke and Invoke methods

@YoshiRulz YoshiRulz added the Working as intended (wontfix) Intended behaviour perceived as a bug, or an unreasonable feature request label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core: Future core Core doesn't exist yet or is an early WIP re: APIHawk Relating to EmuHawk's public .NET API or to the creation of external tools Repro: Affects 2.9.2 dev Working as intended (wontfix) Intended behaviour perceived as a bug, or an unreasonable feature request
Projects
None yet
Development

No branches or pull requests

3 participants