After socket.CloseAsync, I got error "TaskCanceledException: A task was canceled."

Hi,

My game is running fine in Unity Editor, everything’s working as expected.
but right after I close a socket with function CloseAsync(), Console will show this error message
"System.Threading.Tasks.TaskCanceledException: A task was canceled." (full error message below)

Anyone know what’s the cause of this error and how to solve this?
Thanks in advance & Best regards,
Bodin

Click for full error Detail

DebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <695d1cc93cca45069c528c15c9fdd749>:0
at Nakama.Ninja.WebSockets.Internal.WebSocketFrameReader+d__2.MoveNext () [0x0035f] in :0
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0001a] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in <695d1cc93cca45069c528c15c9fdd749>:0 at Nakama.Ninja.WebSockets.Internal.WebSocketImplementation+<ReceiveAsync>d__34.MoveNext () [0x00198] in <c17a258af24e4d289a30070aa95dc0df>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <695d1cc93cca45069c528c15c9fdd749>:0 at Nakama.Ninja.WebSockets.Internal.WebSocketImplementation+<ReceiveAsync>d__34.MoveNext () [0x004a7] in <c17a258af24e4d289a30070aa95dc0df>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <695d1cc93cca45069c528c15c9fdd749>:0 at Nakama.Ninja.WebSockets.Internal.WebSocketImplementation+<ReceiveAsync>d__34.MoveNext () [0x00909] in <c17a258af24e4d289a30070aa95dc0df>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <695d1cc93cca45069c528c15c9fdd749>:0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0001a] in <695d1cc93cca45069c528c15c9fdd749>:0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <695d1cc93cca45069c528c15c9fdd749>:0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <695d1cc93cca45069c528c15c9fdd749>:0 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <695d1cc93cca45069c528c15c9fdd749>:0
at Nakama.WebSocketAdapter+d__35.MoveNext () [0x000c5] in :0
UnityEngine.Debug:LogError (object)
Nakama.Socket:<.ctor>b__75_2 (System.Exception)
Nakama.WebSocketAdapter/d__35:MoveNext ()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<System.Net.WebSockets.WebSocketReceiveResult>:SetException (System.Exception) Nakama.Ninja.WebSockets.Internal.WebSocketImplementation/<ReceiveAsync>d__34:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Nakama.Ninja.WebSockets.Internal.WebSocketReadCursor>:SetException (System.Exception)
Nakama.Ninja.WebSockets.Internal.WebSocketFrameReader/d__2:MoveNext ()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder:SetResult ()
Nakama.Ninja.WebSockets.Internal.BinaryReaderWriter/d__0:MoveNext ()
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

Hey @bodin could you share the code you’ve written to close the socket? For example, is it being invoked in an async Unity lifecycle function?

Hi @lugehorsam,

Thank you for your reply.
I close the socket when player quit the game.

async void OnApplicationQuit()
{        
    if (socket != null)
    {            
        await socket.CloseAsync();
    }        
}

Here is the list of what I done for solving the issue, none of them work.

  • run client.SessionLogoutAsyn before closing the socket.
  • run socket.LeaveMatchAsync before closing the socket.

Can you ensure you are on nakama-unity v3.4.1? We recently made changes to the socket lifecycle in that release.

@lugehorsam
yes, I already updated to v3.4.1
I did that to solve the problem about CreateMatchAsync(matchName) overload.
( This is my previous post about it
Cannot provide a name when creating a new match with CreateMatchAsync)

Do you mean this error happen in v3.4.1?

No it shouldn’t happen in that release.

I’m having trouble reproducing your issue – can you share you’re using the socket? What sorts of operations are you performing and where in your application?

Update: I was able to reproduce this issue. This shouldn’t be a blocking issue (i.e., it only happens after the editor closes) but we’ll prioritize it for the next release.

2 Likes

Thank you very much for the update.

Please don’t worry, This is not a serious issue (for now). My game still running fine.
Hopefully to see it solved in next release.

I see that error all the time too. I’ve noticed that socket.CloseAsync() can take a really long time to complete, often many seconds. This gave us lots of problems so now we just “fire and forget” that call:

// NOTE: this closing seems to take a long time and we can end up getting a reconnect already in progress when
// this call is finally done and thus we reset data that is again valid. So let the closing perform in the
// background and just drop the socket
try {
    connection.Socket.CloseAsync();
}
catch (Exception ex) {
    // ignore any error from this old socket, we don't care anymore
}

connection.Socket = null;
connection.Session = null;

The closing should really be instant, if something has to be cleaned up internally by the SDK that’s not my problem but the SDK should really handle it in the background.

2 Likes

Thank you very much for your sharing.
but even though I wrapped it with in try-catch, the error message still appearing in the console.

This is my code

        try
        {
            Debug.Log("Closing the socket");
            await NakamaUserData.Instance.socket.CloseAsync();                
        }
        catch (System.Exception ex)
        {
            Debug.LogError("to error message : YOU SHALL NOT PASS!");
        }

and this is the result.
error