-
Notifications
You must be signed in to change notification settings - Fork 748
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
Unity get stuck #526
Comments
PS: This code works properly on a simple console application. |
I've tried to look into these issues myself; I have no concrete answers at this time, but I do have some guidance to help try to isolate what's happening. I've started building the project from GitHub and haven't used the Nuget repositories in a while so I can't say whether those are working or not. So I'd suggest doing a Github pull of the project and building the NetMQ35 project locally.
Would love to hear back from you if you do try any of the above. Thanks and Good Luck |
Thank you Mark, I tried these:
3, 4) Doesn't worked.
I cleared all sending and receiving codes but the issue remains: using UnityEngine;
using System;
using NetMQ;
using NetMQ.Sockets;
public class ReqRep : MonoBehaviour
{
// Use this for initialization
void Start()
{
Debug.Log("Start.");
AsyncIO.ForceDotNet.Force();
using (ResponseSocket rep = new ResponseSocket("tcp://192.168.1.4:8833"))
{
}
Debug.Log("End.");
}
} Thanks, |
I also tried to figure this out, the best I can suggest is to use version 3.X and use NetMQContext, make sure to close it when the application exit. I will add a method to kill the content the contextless NetMQ 4.0 is using. |
Hi Hojjat, Oh, and as @somdoron mentioned; I am using the earlier "NetMQContext" method; not the more recent "Contextless" versions. But that's more because the code I had was built before the "Contextless" approach existed. To make this work on multiple objects I made a single gameObject which created the context ("NetworkBus") and all other scripts located the NetworkBus and requested the Context object that it created. I've never been able to make Unity exit without crashing if I created a TCP socket... I'd love to hear back if you were able to not crash on exit. :) What I meant was rather than do both request and response in the same object on two sockets; just put one side in the start method (a request version, then a response version). The request version would be:
The response version would be:
Also, I've had success using the "inproc" method to communicate to gameobjects within Unity but that's obviously just communicating within the single Unity process; not outside to other applications. |
With the new version, since the Context termination only happens when the application exit's (NetMQConfig line 22), in Unity we never see this happen in the editor, that's the reason for not working. One way is to patch the NetMQConfig to allow for a call to Ctx.Terminate, but not a good practice for applications outside the Unity3D world. |
Thank you @dgsantana, it was better if we had some control over context termination. |
Are the resources (like the listening thread/process) for multiple NetMQ
|
I did some patching on my local branch, and even a "clear" call doesn't seem to solve totally. Because you also need to create the Context for the next Unity3D editor play, and I also did that. But there are more threading issues that I couldn't find yet. |
That sounds like a great start! Deeper than I was able track! Great work! I'm a bit confused though because Clear() should leave the Context alone However there's an alternative; what if you register/Request a Named Each object would use RequestContext("contextName") to get a ctx object or The contextname would pull the context from a dictionary managed by NetMQ. If NetMQ was providing a way to register a context a refer to/start/stop it ? Mike
|
Sounds like an interesting approach @MikeFair . But may require some rewrite of NetMQ new code to pass a named context's, in the socket's constructor (optionally). I will track the use of the internal NetMQConfig.Context to see how deep is the rabbit hole. My solution was just in the NetMQConfig. I added to 3 new methods, one was RequiresManualContextTermination, that "disables" the ProcessExit event registered on the static constructor, a ManualTerminateContext(bool block=true) that terminates the context, setting if it should block or not, and a ManualCreateContext(), for creating a context (needed when you terminate, and need to create a new one, e.g when pressing play the second time in the Unity Editor). I do like the new contextless system, and use it outside Unity, it's just in the Unity Editor that there is need for some extra work on the NetMQ side. |
For any one wanting to test this approach, I made it available here: I also added some simple test and they pass, but they are simple. Sockets that aren't close, weren't tested yet. |
@dgsantana can you make a pull request? |
@dgsantana Thanks! This is working for me! I would like to see a pull request. 😄 |
I think it is already in...
|
Yeap it's in. A Sex, 24 de jun de 2016, 12:54, Doron Somech [email protected]
|
Oh I thought it isn't in because of the open issue and there was no answer. |
@dgsantana A minimal code I wrote is the following: // Attach this script on a GameObject in Unity3D and play in the editor
// --> the editor gets stuck at ContextTerminate() below
using UnityEngine;
using NetMQ; // for NetMQConfig
using NetMQ.Sockets;
public class NetMQUnityTest: MonoBehaviour
{
void Start()
{
NetMQConfig.ManualTerminationTakeOver();
NetMQConfig.ContextCreate(true);
using (var req = new RequestSocket())
{
req.Connect("tcp://127.0.0.1:50020");
req.SendFrame("Hello");
req.Close(); // testing with/without this line did not change the behavior.
}
// Unity editor gets stuck at this line
NetMQConfig.ContextTerminate(); // giving 'true' as an arg. did not change the behavior.
}
} I suspect I'm misusing your utility functions, so I really appreciate if you can point out any faults in my test code... I compiled NetMQ.dll and AsyncIO.dll from the master branch of NetMQ with VC2015, and placed in a Unity's asset folder. |
You still have to use
|
@PolynomialDivision using UnityEngine;
using System.Threading;
using System.Collections;
using System.Timers;
using NetMQ; // for NetMQConfig
using NetMQ.Sockets;
public class NetMQUnityTest: MonoBehaviour
{
Thread client_thread_;
private Object thisLock_ = new Object();
bool stop_thread_ = false;
void Start()
{
Debug.Log("Start a request thread.");
client_thread_ = new Thread(NetMQClient);
client_thread_.Start();
}
// Client thread which does not block Update()
void NetMQClient()
{
AsyncIO.ForceDotNet.Force();
NetMQConfig.ManualTerminationTakeOver();
NetMQConfig.ContextCreate(true);
string msg;
var timeout = new System.TimeSpan(0, 0, 1); //1sec
Debug.Log("Connect to the server.");
var requestSocket = new RequestSocket(">tcp://192.168.11.36:50020");
requestSocket.SendFrame("SUB_PORT");
bool is_connected = requestSocket.TryReceiveFrameString(timeout, out msg);
while (is_connected && stop_thread_ == false)
{
Debug.Log("Request a message.");
requestSocket.SendFrame("msg");
is_connected = requestSocket.TryReceiveFrameString(timeout, out msg);
Debug.Log("Sleep");
Thread.Sleep(1000);
}
requestSocket.Close();
Debug.Log("ContextTerminate.");
NetMQConfig.ContextTerminate();
}
void Update()
{
/// Do normal Unity stuff
}
void OnApplicationQuit()
{
lock (thisLock_)stop_thread_ = true;
client_thread_.Join();
Debug.Log("Quit the thread.");
}
} The above code does not freeze the editor after playing. It also doesn't block its Update function. |
@YutaItoh I was able to use your code with custom built NetMQ3.5 (from latest master), so thanks! I replaced obsolete methods with suggested ones. How would I use netMQ if I need multiple sockets? Like getting messages from one address (1 instance of socket) and sending messages to a different address (X instances of sockets)? Unity (5.3.4f) gets stuck whenever there is more then one socket open (different monobehaviours) [exiting play mode], or while trying to use debugger :(. Should I build from some old revision of netMQ master? Or is there some specific way how to treat Unity to avoid freezing? Changes to above mentioned code: protected override void NetMQClient() { AsyncIO.ForceDotNet.Force(); NetMQConfig.Cleanup();
Or is it possible to get somewhere the net35 compatible versions of AsyncIO.dll and NetMQ.dll (as it was mentioned above as important)? I'm not able to open AsyncIO sln for some reason in VS 2015 and so I don't know if it was resolved correctly by nuget while building NetMQ (I have no real experience with nuget). |
Why are you doing a NetMQConfig.Cleanup() at the beginning?
I had problems building the librarys with VS 2015, too. As I unterstood it you should just make a cleanup in the end when you close the last socket. |
@PolynomialDivision I used the above code and as the method is obsolete replaced it with the suggestion:
|
I've been playing around with this issue for a bit and managed to trace the problem down a bit further. So in my particular use case scenario, I'm mainly testing/debugging inside Unity's editor. I have more than one socket open and everything merrily connects and operates as expected. When I STOP the editor I start to call my cleanup code to close all my sockets/pollers and finish up with After experimenting with a slightly earlier copy of NetMQ that allowed me to step through the debugger and figure out what was hanging, I noticed that the Approaching from the opposite direction, I tracked the Socket destruction code to the My workaround for this issue is to force-quit Unity and relaunch. My exported builds don't experience this issue as long as I drop |
I had some issues where unity would hang if scripts started rebuilding while I was playing but that was solved as described in this answer: https://answers.unity.com/questions/1246172/is-there-an-event-triggered-before-editor-starts-r.html |
Using the latest Nuget version of the 35 libs in Unity 5.4.2p3, I'm still seeing the Unity editor lock up quite frequently. I made a very simple Dealer connection that works fine until I add a NetMQPoller into the mix. If I add that, the Unity editor locks up when I hit stop.
|
@Supergeek from your pasted lines it is not clear if are you properly stopping / disposing
|
I know this is an old thread but for people running in the freeze problem after 1 time use. The code @YutaItoh provided didn't work for me anymore. Maybe because my messages are fairly large. I've still used some of Yutaltoh's code so still thanks to him!
Hope this helps some people. The only slight problem this has is that it'll require a next message in order to quite properly. |
I have the same issue, It's not even the Second call to EDIT Ok.. well my problem with the above example was I was using a |
As I recall, the problem was triggered by binding a listening socket; you
could create Sockets that connected to other hosts okay, but could not
receove them without causing the lock-up... It's been a while so I might be
misremembering the exact causes (but I do remember that there were
behaviors that would exit cleanly at the time).
…On Oct 9, 2017 10:37 AM, "Adam Venturella" ***@***.***> wrote:
I have the same issue, Unity 2017.1.1f1. The above did not resolve it.
It's not even the Second call to Play that locks it. Attempting to Quit
Unity after running it once will still cause Unity to lock up.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#526 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACMqLbZT1I9nW_2CID8-7hKhjeBLqHIbks5sqlnKgaJpZM4IHM7O>
.
|
Had this issue with Unity 2018.3.12 with the editor being stuck at I used this NuGet For Unity Utility to add this NetMQ.Unity package to my project instead of building from github source. Adding |
Here is a working REQ-REP helloworld code sample for unity2018.3.2 on Win10,
REP Server in Unity
REQ client in python:
|
Also a REQ-REP with both client & server on Unity:
|
This issue has been automatically marked as stale because it has not had activity for 365 days. It will be closed if no further activity occurs within 56 days. Thank you for your contributions. |
I created a simple Request/Reply sockets in Unity 5.3.1 but it only works for the first time, the second time that I press Play button the Unity hangs and I need to close it using Windows task manager.
I build the NetMq using the last source code, here is my script.
The text was updated successfully, but these errors were encountered: