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

isClient is false in OnDestroy #3832

Closed
Ghetaldus opened this issue Jun 1, 2024 · 11 comments
Closed

isClient is false in OnDestroy #3832

Ghetaldus opened this issue Jun 1, 2024 · 11 comments

Comments

@Ghetaldus
Copy link

Describe the bug
When player disconnects isClient is false in OnDestroy on dedicated server.

[IMPORTANT] How can we reproduce the issue, step by step:
Please tell us how to reproduce your issue, STEP BY STEP, with one of our built in examples.

  • Import latest Mirror version from git
  • Open scene MirrorBasic from Basic example
  • Open Player script from Basic example and add OnDestroy with debugs, like so for example:
void OnDestroy()
{
    Debug.Log("isClient " + isClient);
    Debug.Log("isLocalPlayer " + isLocalPlayer);
    Debug.Log("isServer " + isServer);
    Debug.Log("NetworkClient.active " + NetworkClient.active);
    Debug.Log("NetworkServer.active " + NetworkServer.active);
}
  • Build it
  • Start in Unity editor as server only
  • Run your build as a client
  • Stop Client and look at the logs in Unity editor console

Expected behavior
isClient should be true in OnDestroy

Desktop (please complete the following information):

  • OS: Windows 10
  • Build target: Standalone, Windows
  • Unity version: 2021.3.35 & 2022.3.21
  • Mirror branch: 89.7.4
ondestroy
@Ghetaldus
Copy link
Author

Also just found that tests for isServer OnDestroy are also failing.

Tests

@Ghetaldus
Copy link
Author

Ghetaldus commented Jun 1, 2024

Here are some more finds I've made while debugging:

  • Destroy in NetworkServer calls on Unspawn(obj) and right after that destroys the object (which calls OnDestroy on these objects)
  • Unspawn(obj) calls on identity.ResetState(), which resets all the bools (isClient, isServer etc) to false
  • That in turn makes all those bools always false for us in OnDestroy
image

@miwarnec
Copy link
Collaborator

Checking!

@miwarnec
Copy link
Collaborator

miwarnec commented Jun 20, 2024

Hey @Ghetaldus maybe I am misunderstanding this.

  • You are starting the server-only
  • You connect a client
  • You disconnect the client
  • You expect IsClient to be 'true' on the server?

That's what you mentioned in the first post.
IsClient is only ever true on 'client', never on 'server', so this behaviour would correct as is no?

If I test the other way around: build=server-only, editor=client, the IsClient flag is true as we would expect:
image

@Ghetaldus
Copy link
Author

Ghetaldus commented Jun 20, 2024

Yeah, you are right there with isClient.
However even isServer flag is cleared out on the server before OnDestroy happens.

You can run tests for OnDestroyIsServerTrue and you will see that these fails.

image

@MrGadget1024
Copy link
Collaborator

@Ghetaldus

Also just found that tests for isServer OnDestroy are also failing.

You're looking at PlayMode tests - we haven't maintained those in ages because CI doesn't run them. The tests themselves are broken.

@MrGadget1024
Copy link
Collaborator

  • Destroy in NetworkServer calls on Unspawn(obj) and right after that destroys the object (which calls OnDestroy on these objects)
  • Unspawn(obj) calls on identity.ResetState(), which resets all the bools (isClient, isServer etc) to false
  • That in turn makes all those bools always false for us in OnDestroy

Override OnStopServer and OnStopClient instead. Also you should be calling NetworkServer.Destroy instead of just Destroy.

@Ghetaldus
Copy link
Author

Thanks Mr. G! I've already started refactoring code to use OnStopServer/OnStopClient, instead of OnDestroy.

@miwarnec
Copy link
Collaborator

I checked the server logs again.
image
You are right, isServer is false there.

@miwarnec
Copy link
Collaborator

call stack:
image

miwarnec pushed a commit that referenced this issue Jun 26, 2024
…abs, fixes isServer flag always being false in OnDestroy
@miwarnec
Copy link
Collaborator

miwarnec commented Jun 26, 2024

fixed:
image

PR: #3848
thanks for reporting @Ghetaldus

miwarnec added a commit that referenced this issue Jun 26, 2024
…abs, fixes isServer flag always being false in OnDestroy (#3848)

* repro

* NetworkServer.Destroy(): refactor to split scene vs. prefab cases more obviously

* fix: #3832 NetworkServer.Destroy now doesn't call ResetState for prefabs, fixes isServer flag always being false in OnDestroy

* fix the runtime test

* Revert "repro"

This reverts commit 3a5a33e.

---------

Co-authored-by: mischa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants