Sign In Register

How can we help you today?

Start a new topic

[Problem] Unity real-time SDK: Program not responding when exiting the game.

Hello!  

  

I got this problem where if the user exits the application (alt+f4 or pressing the window "red cross" button) the game hangs up and I get "Program is not responding".


This is after GameSparks has been initalized and connected,

I think I have to somehow disconnect the program from gamesparks servers before closing it.


But how would I go about doing this?


I've tried:

private void OnApplicationQuit()

 {

  GS.Disconnect();

  GS.ShutDown();

  Destroy(GameSparksManager.Instance());

 }


Other than that, GameSparks realtime SDK is really awesome and fits my needs perfectly!


Thanks in advance for any response!

- Simon


1 person has this question

Also, I set up my project using the Unity real time tutorial.

I have been having this issue; was anyone able to find a solution to this?

I have tried all the usual solutions like re-importing the SDK and trying to close the match, and service, down prior to the Window attempting to close. But 3 of 4 windows hang until I brute force them to close.

I too have been following the Unity Tutorial with the 4 tanks; any help would be very much appreciated? Other than this issue, I have had a great experience learning GameSparks system.

If found a very ugly workaround:
if (!Application.isEditor) System.Diagnostics.Process.GetCurrentProcess().Kill();


Hello.

I have the same problem, most of the times the program (build ) stop responding when I click the exit button, have you guys found a solution? And I hope that the support team can check this issue.

Hello 

I Have the same problem.

have your found a solution ?

Nope, I haven't found a solution to this yet. It is veeery annoying! Would be helpful with a answer from an actual developer!

This is concerning. Is this on Windows build? Are you seeing these issues on mobile too?

Hoping someone from GameSparks can comment


Haven't tested it on a mobile build yet.

Alright, so I managed to fix it. What I did was to check if gamesparks is connected in a OnApplicationQuit method.

If it is available (meaning you are still connected = game freezes), we need to stop the application from quitting and wait until you are disconnected before we try to shut the application down again. 


I am at work now, so I can't check exactly what I wrote, but you can try and copy paste this code to see if it works for you (there may be issues):

 

private bool _allowQuitting;

public void OnApplicationQuit()
{
	if (GS.Available)
	{
		_allowQuitting = false;
		StartCoroutine(DelayedQuit());
	}
	
	if (!_allowQuitting)
	{
		Application.CancelQuit();
	}
}

private IEnumerator DelayedQuit()
{	
	//Now disconnecting GS.services, not sure if all these lines are needed, but this is what I have and it works
	GS.Disconnect();
	GS.ShutDown();
	
	//Not sure if this line of code is needed:
	if(GameSparksManager.Instance().GetRTSession() != null)
		GameSparksManager.Instance().GetRTSession().Disconnect();
	new EndSessionRequest().Send((response) => {
		GSData scriptData = response.ScriptData;
		long? sessionDuration = response.SessionDuration;
	});
	
	//Waiting 1 second before quitting game, so we can disconnect client
	//A better approach is to create a while-loop, checking if GS.Available
	yield return new WaitForSeconds(1);
	
	while(GS.Available)
	{
		_allowQuitting = false;
		yield return null;
	}
	_allowQuitting = true;
	
	Application.Quit();
}

 Not sure if it is exactly how I wrote it, but it is something like that. But it should work. 
Tell me if you have any issues, and I will rewrite the code so it works. 


1 person likes this

Hey all,


I've encountered the same problem as well. I am only building for Windows so I cannot help with other platforms. It's possible that it works perfectly fine in non-Windows builds. I am using SDK Version 5.5.5.114 and building x64.


I used Simon's code (thanks!) and removed some unneeded things (or so I think they are unneeded):

    private void OnApplicationQuit()
    {
        if (!m_isCleaningUp)
        {
            m_isCleaningUp = true;           

            if (GS.Available)
            {
                Application.CancelQuit();
                StartCoroutine(CleanUpGameSparks());
            }
            else
            {
                m_isSafeToQuit = true;
            }
        }

        if (!m_isSafeToQuit)
        {
            Application.CancelQuit();
        }
    }

    // there is something in the GameSparksUnity component that causes the game to hang when quitting
    // hence why this needs to be here
    private IEnumerator CleanUpGameSparks()
    {
        GS.ShutDown();

        // without a wait, the hanging still occurs
        yield return new WaitForSecondsRealtime(1.0f);

        while (GS.Available)
        {
            yield return new WaitForSecondsRealtime(0.1f);
        }

        m_isSafeToQuit = true;
        Application.Quit();
    }

 

Some Research


It really bugs me that I don't know what is going wrong with something I didn't write so I looked into the GameSparks SDK code a bit:   

// GameSparksUnity component
void Start()
{

#if ((UNITY_SWITCH || UNITY_XBOXONE || (WINDOWS_UWP && ENABLE_IL2CPP)) && !UNITY_EDITOR) || GS_FORCE_NATIVE_PLATFORM
    this.gameObject.AddComponent<NativePlatform>();
#elif UNITY_WEBGL && !UNITY_EDITOR
    this.gameObject.AddComponent<WebGLPlatform>();
#else
    this.gameObject.AddComponent<DefaultPlatform>();
#endif
//		System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
//		GameSparksWebSocket.Proxy = new System.Net.DnsEndPoint2("localhost", 8888);
//		GS.TraceMessages = true;
//		GameSparks.Core.GameSparksUtil.LogMessageHandler = Debug.Log;
#if UNITY_IOS && !UNITY_EDITOR
    GSGetProxySettings(this.name);
#endif
}

// PlatformBase component (DefaultPlatform inherits from PlatformBase)
virtual protected void Update(){

    lock (_actions)
    {
        if (_actions.Count > 0) {
            _currentActions.AddRange (_actions);
            _actions.Clear ();
        }
    }
    var count = _currentActions.Count;

    if (count > 0) {
        for (var index = 0; index < count; ++index) {
            var a = _currentActions [index];
            if (a != null) {
                a ();
            }
        }

        _currentActions.Clear ();
    }
}

virtual protected void OnApplicationQuit(){
    GS.Disconnect();
}

virtual protected void OnDestroy () {
    Update();
    GS.ShutDown();
}

   From these components, it doesn't seem like anything could be happening to cause the freeze. I'm thinking that a socket is still open in the WebSocketController component. I didn't dig into that class too much but it sounds plausible that a socket is still holding resources at the time of close.


I might spend some time looking around some more but I can confirm that my game doesn't hang any more on quit.


Hope this helps you all.


Best,

Chris

Here's another post I just replied to with some updates I made to the script: https://support.gamesparks.net/support/discussions/topics/1000082137/page/last#post-1000211089

I'm having this issue on Unity 2017.3.1f1 with GameSpark SDK 5.6.3.205


As soon as any GameSparks connection is made it's impossible to quit my game (built on/for Windows 64bit), it freezes when Application.Quit() is executed.


I've tried all the scripts in this thread, nothing helps.


The only way to quit the game is to use Windows Task Manager and "End Task".


I'm able to quit the game fine if I comment out my connection (I'm currently only using GameSparks.Api.Requests.DeviceAuthenticationRequest() ).


If the DeviceAuthentication is executed then my game can never be quit.

Same issue with Unity 2017.3.1f1 and SDK V5.6.3.205 - March 7th, 2018


I have fixed my problem, I hope anyone else finding this has the same issue/solution. Please GameSparks put this fix in your next release, it's super simple...


My game uses Time.timeScale = 0 to pause the game (Like I assume many, most, all other games do) and this doesn't necessarily get set back to 1 until the game restarts. That's all fine, except for this wonderful block of code in GameSparks\Platforms\PlatformBase.cs :


    

private bool _allowQuitting = false;

virtual protected void OnApplicationQuit(){
    GS.ShutDown();
    StartCoroutine("DelayedQuit");

    if (!_allowQuitting){
        Application.CancelQuit();
    }
}

IEnumerator DelayedQuit(){
    yield return new WaitForSeconds(1.0f);
    while (GS.Available) {
        yield return new WaitForSeconds(0.1f);
    }

    _allowQuitting = true;
    Application.Quit(); 
}

  This looks pretty much like the fix shared by Simon and Christopher, except for some reason they swapped WaitForSecondsRealtime with WaitForSeconds, which means my paused game will never, ever execute the rest of their code. It's super simple to fix yourself, or just use this:


 

private bool _allowQuitting = false;

virtual protected void OnApplicationQuit(){
    GS.ShutDown();
    StartCoroutine("DelayedQuit");

    if (!_allowQuitting){
        Application.CancelQuit();
    }
}

IEnumerator DelayedQuit(){
    yield return new WaitForSecondsRealtime(1.0f);
    while (GS.Available) {
        yield return new WaitForSecondsRealtime(0.1f);
    }

    _allowQuitting = true;
    Application.Quit(); 
}

   Awful lot of time spent tracking this down, as GS gives NO debug messages about never being able to shut itself down. This seems like an oversight for something that can potentially make a game completely unresponsive.

Login to post a comment