Unity Client freezing on connect

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Unity Client freezing on connect

Postby HumanPoweredGames » 26 Jun 2012, 16:51

I am having an intermittent problem with Unity (Editor and players) freezing when I connect to my server.

I have a repro case that I submitted to Unity, and they say that the crash logs indicate that it looks like something happening in the SmartFox dll.

Has anyone else been having this problem?

Edit: The following code will freeze Unity after a few minutes... sometimes sooner. It seems to be random.

Code: Select all

using Sfs2X;
using Sfs2X.Entities;
using Sfs2X.Logging;

//NetworkTest.cs
public class NetworkTest : MonoBehaviour
{      
   IEnumerator Start()
   {
      while(true)
      {   
         SmartFox smartFox = new SmartFox();
         smartFox.Connect("YOUR HOST", 9933);
         print("Connected");
         yield return new WaitForSeconds(0.02f);
         
         smartFox.Disconnect();
         print ("disconnected");
         yield return new WaitForSeconds(0.02f);
      }

   }   
}
User avatar
Lapo
Site Admin
Posts: 21092
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity Client freezing on connect

Postby Lapo » 27 Jun 2012, 09:26

Why do you call Connect() without setting up the relative event listener?
Lapo
--
gotoAndPlay()
...addicted to flash games
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 27 Jun 2012, 14:41

This code is a stripped down demo of a random crashing problem I have in my project. I do set up the event listener in my project.

It seems to lock up more often in my project. I end up restarting Unity 10 - 12 times an hour.
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 27 Jun 2012, 15:33

I added an event listener, but Unity still freezes up:

Code: Select all

using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Requests;
using Sfs2X.Logging;
using Sfs2X.Entities.Data;


//NetworkTest.cs
//====================================================================================================================//
public class NetworkTest : MonoBehaviour
{   
   SmartFox smartFox;
   void Start()
   {
         smartFox = new SmartFox();
         

         smartFox.AddEventListener(SFSEvent.CONNECTION, delegate(BaseEvent evt)
         {   
            print("Connected");      
            smartFox.Disconnect();
            print ("disconnected");
            Start();
         });

         smartFox.Connect("databar.com", 9933);
         
   }   
   
   
   void Update()
   {
      
      if(smartFox != null)
         smartFox.ProcessEvents();
   }
   
   
}
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 28 Jun 2012, 14:33

Got an email notification that there was a new post in this thread, but I don't see a new post here?
User avatar
A51Integrated
Posts: 240
Joined: 03 Jan 2012, 19:55
Location: Toronto, Canada
Contact:

Re: Unity Client freezing on connect

Postby A51Integrated » 28 Jun 2012, 21:24

That was most likely a spam message and one of the admin deleted it before you got a chance to view it. Lovely how spammers waste our time eh? :wink:
A51 Integrated
http://a51integrated.com / +1 416-703-2300
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 29 Jun 2012, 03:14

I got all excited :(
User avatar
Lapo
Site Admin
Posts: 21092
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity Client freezing on connect

Postby Lapo » 29 Jun 2012, 06:59

Does this happen with our examples too? It should really not.
Please check one of the simplest such as the Connector and try that. If you still see the same issue please post here all the details such as Unity version, SFS2X API version etc...
Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 30 Jun 2012, 02:43

I am running server version 2.1.0, and my Unity client DLL is 1.0.3.

I added a coroutine 'Abuser()' to the GUI script which automatically disconnects and reconnects. If I run the script in the Unity editor, it gradually runs more and more slowly. If I build a standalone mac app with the Connection GUI, it runs for a little while and freezes up the player, which can then only be closed with Force Quit.

Edit: the editor will freeze up too... it just takes longer.

Below is my modified version of ConnectionGUI.cs from the Connector example:


Code: Select all

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Logging;

public class ConnectionGUI : MonoBehaviour
{
   //----------------------------------------------------------
   // Setup variables
   //----------------------------------------------------------
   public string serverName = "127.0.0.1";
   public int serverPort = 9933;
   public LogLevel logLevel = LogLevel.DEBUG;
   public GUISkin sfsSkin;
   
   private string statusMessage = "";
   private SmartFox smartFox;
   private List<string> logMessages = new List<string>();
   private Vector2 logScrollPosition;   
   
   //----------------------------------------------------------
   IEnumerator Abuser()
   {
      while(true)
      {   
         if(smartFox.IsConnected)
            smartFox.Disconnect();
         else
            smartFox.Connect(serverName, serverPort);      
         
         yield return new WaitForEndOfFrame();
      }
      
   }
   
   //----------------------------------------------------------
   // Called when program starts
   //----------------------------------------------------------
   void Start()
   {
      // In a webplayer (or editor in webplayer mode) we need to setup security policy negotiation with the server first
      if (Application.isWebPlayer /*|| Application.isEditor*/) {
         if (!Security.PrefetchSocketPolicy(serverName, serverPort, 500)) {
            Debug.LogError("Security Exception. Policy file load failed!");
         }
      }      

      // Register for basic callbacks
      smartFox = new SmartFox(true);
      smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
      smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
      
      // Also register for all debug messages from the API at the given log level
      smartFox.AddLogListener(logLevel, OnDebugMessage);
      
      statusMessage = "Not connected";
      logMessages.Add("Log Messages:");
      
      StartCoroutine("Abuser");
   }
   
   //----------------------------------------------------------
   // As Unity is not thread safe, we process the queued up callbacks every physics tick
   //----------------------------------------------------------
   void FixedUpdate() {
      smartFox.ProcessEvents();
   }
   
   //----------------------------------------------------------
   // Draw GUI every frame
   //----------------------------------------------------------

   void OnGUI() {
      if (smartFox == null) return;

      GUI.skin = sfsSkin;
      
      // Lets just quickly set up some GUI layout variables
      float panelWidth = 400;
      float panelHeight = 300;
      float panelPosX = Screen.width/2 - panelWidth/2;
      float panelPosY = Screen.height/2 - panelHeight/2;
      
      // Draw the box
      GUILayout.BeginArea(new Rect(panelPosX, panelPosY, panelWidth, panelHeight));
      GUILayout.Box ("Connector Example", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
      GUILayout.BeginVertical();
      GUILayout.BeginArea(new Rect(20, 25, panelWidth-40, panelHeight-100), GUI.skin.customStyles[0]);

      // Show server log inside
      logScrollPosition = GUILayout.BeginScrollView(logScrollPosition, false, false);
      
      foreach (string message in logMessages) {
         GUILayout.Label(message);
      }
      
      GUILayout.EndScrollView();
      GUILayout.EndArea ();      
      
      GUILayout.BeginArea(new Rect(20, panelHeight-70, panelWidth-40, 80));
      
      // Display client status
      GUIStyle centeredLabelStyle = new GUIStyle(GUI.skin.label);
      centeredLabelStyle.alignment = TextAnchor.MiddleCenter;
      
      GUILayout.Label("Client Status: " + statusMessage, centeredLabelStyle);
      
      // Center button
      GUILayout.BeginHorizontal();
      GUILayout.FlexibleSpace();      
      if (!smartFox.IsConnected) {
         // Connect button
         if (GUILayout.Button("CONNECT")) {
            smartFox.Connect(serverName, serverPort);      ;
         }
      }
      else {
         // Show disconnect button
         if (GUILayout.Button("DISCONNECT")) {
            smartFox.Disconnect();
         }
      }
      GUILayout.FlexibleSpace();
      GUILayout.EndHorizontal();
      
      GUILayout.EndArea ();      
      GUILayout.EndVertical();
      GUILayout.EndArea ();
   }

   //----------------------------------------------------------
   // Handle connection response from server
   //----------------------------------------------------------
   public void OnConnection(BaseEvent evt) {
      bool success = (bool)evt.Params["success"];
      if (success) {
         statusMessage = "Connection succesful!";
      } else {
         statusMessage = "Can't connect to server!";
      }
   }

   public void OnConnectionLost(BaseEvent evt) {
      statusMessage = "Connection was lost, Reason: " + (string)evt.Params["reason"];
   }
   
   //----------------------------------------------------------
   // Show the debug messages both in window as well as console log
   //----------------------------------------------------------
   public void OnDebugMessage(BaseEvent evt) {
      string message = (string)evt.Params["message"];
      logMessages.Add(message);
      Debug.Log("[SFS DEBUG] " + message);
   }
   
   //----------------------------------------------------------
   // Disconnect from the socket when shutting down the game
   //----------------------------------------------------------
   public void OnApplicationQuit() {
      smartFox.Disconnect();
   }
}
User avatar
Lapo
Site Admin
Posts: 21092
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity Client freezing on connect

Postby Lapo » 30 Jun 2012, 07:03

My question again, does this problem happen if you use our Connector example out of the box?
I have been using that example and the others so many times and I have never seen a single problem.
Lapo

--

gotoAndPlay()

...addicted to flash games
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 30 Jun 2012, 16:01

In order to make it happen out of the box, I would have to sit there and click the button for 8 or 10 minutes, as would you to reproduce the problem. I have not been able to sustain the patience for this endeavor, so I modified the script to click the button for me. It does exactly what the script would otherwise.

Again, this is an intermittent problem. Sometimes I will go for several hours with no problems, and then it will lock up every third time I connect. The only way I know to make it reliably reproducible is to run a loop that connects and disconnects. Above are 3 different ways to reproduce the problem. I can also give you a build of my game for a 4th way to reproduce the problem.

Is there anything in any of these examples that should cause Unity to crash? Have you run any of them?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Re: Unity Client freezing on connect

Postby ThomasLund » 03 Jul 2012, 09:38

Emailed you directly as well, but just to respond here as well.

Yes - seeing the issue as well, and it seems to be an issue that was identified some weeks ago by Unity guys as a "feature" in Mono. Seems Mono doesnt always detect thread termination and then waits indefinitely for an already terminated thread to terminate.

Unity posted some workarounds. Will try those and see if we can fix it.

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
HumanPoweredGames
Posts: 71
Joined: 19 Oct 2011, 16:40
Location: Alaska
Contact:

Re: Unity Client freezing on connect

Postby HumanPoweredGames » 03 Jul 2012, 15:38

Thanks, Thomas!
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Re: Unity Client freezing on connect

Postby ThomasLund » 04 Jul 2012, 13:34

And we think we fixed it. Been running all day without a single hang of any kind.

Russ - you got mail - please test in your end as well and if its OK, we can get it into next update

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Aldenea, DavinVicy, DerikFent, Keganniff, Kevengar and 13 guests