Ensure condition failed: CurrentNum == InitialNum Coming from an UnknownFunction in UnrealEditor-WebSockets.dll

Hello.

I am using Unreals WebSockets module to connect to a websocket server.

I have a lot of code around this, but essentially my websocket is declared like so:

TSharedPtr<IWebSocket> WebSocket;

And then is initialized like so

WebSocket = FWebSocketsModule::Get().CreateWebSocket(serverUrl);

if (WebSocket.IsValid()) {
		WebSocket->OnMessage().AddLambda([this](const FString& data)
			{
				//callback code
			});

		WebSocket->OnRawMessage().AddLambda([this](const void* Data, SIZE_T Size, SIZE_T)
			{
				//callback code
			});

		WebSocket->OnConnected().AddLambda([this]()
			{
				//callback code
			});

		WebSocket->OnClosed().AddLambda([this](uint32 StatusCode, const FString& Reason, bool bWasClean)
			{
				//callback code
			});

		WebSocket->OnConnectionError().AddLambda([this](const FString& reason)
			{
				//callback code
			});

	}
	else {
		//report error
	}

And then there is a call to connect like so:

WebSocket->Connect();

So the problem is, I want to have 2 different websocket connections. I am able to connect to my first without any problem, but when trying to connect the second, I get this big error:

[2024.01.23-21.03.33:333][319]LogOutputDevice: Error: === Handled ensure: ===
[2024.01.23-21.03.33:336][319]LogOutputDevice: Error: 
[2024.01.23-21.03.33:339][319]LogOutputDevice: Error: Ensure condition failed: CurrentNum == InitialNum  [File:D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 256] 
[2024.01.23-21.03.33:343][319]LogOutputDevice: Error: Array has changed during ranged-for iteration!
[2024.01.23-21.03.33:346][319]LogOutputDevice: Error: Stack: 
[2024.01.23-21.03.33:350][319]LogOutputDevice: Error: [Callstack] 0x00007ff9e899b366 UnrealEditor-WebSockets.dll!UnknownFunction []
[2024.01.23-21.03.33:353][319]LogOutputDevice: Error: [Callstack] 0x00007ff9e899a4bd UnrealEditor-WebSockets.dll!UnknownFunction []
[2024.01.23-21.03.33:356][319]LogOutputDevice: Error: [Callstack] 0x00007ffa16566870 UnrealEditor-Core.dll!UnknownFunction []
[2024.01.23-21.03.33:361][319]LogOutputDevice: Error: [Callstack] 0x00007ffa1658541b UnrealEditor-Core.dll!UnknownFunction []
[2024.01.23-21.03.33:364][319]LogOutputDevice: Error: [Callstack] 0x00007ffa16551778 UnrealEditor-Core.dll!UnknownFunction []
[2024.01.23-21.03.33:367][319]LogOutputDevice: Error: [Callstack] 0x00007ffa1656508e UnrealEditor-Core.dll!UnknownFunction []
[2024.01.23-21.03.33:370][319]LogOutputDevice: Error: [Callstack] 0x00007ffa16566870 UnrealEditor-Core.dll!UnknownFunction []
[2024.01.23-21.03.33:374][319]LogOutputDevice: Error: [Callstack] 0x00007ffa1658541b UnrealEditor-Core.dll!UnknownFunction []
[2024.01.23-21.03.33:377][319]LogOutputDevice: Error: [Callstack] 0x00007ff6d432cea9 UnrealEditor.exe!UnknownFunction []
[2024.01.23-21.03.33:380][319]LogOutputDevice: Error: [Callstack] 0x00007ff6d434d28c UnrealEditor.exe!UnknownFunction []
[2024.01.23-21.03.33:383][319]LogOutputDevice: Error: [Callstack] 0x00007ff6d434d37a UnrealEditor.exe!UnknownFunction []
[2024.01.23-21.03.33:387][319]LogOutputDevice: Error: [Callstack] 0x00007ff6d4350854 UnrealEditor.exe!UnknownFunction []
[2024.01.23-21.03.33:390][319]LogOutputDevice: Error: [Callstack] 0x00007ff6d4366984 UnrealEditor.exe!UnknownFunction []
[2024.01.23-21.03.33:394][319]LogOutputDevice: Error: [Callstack] 0x00007ff6d4369d7a UnrealEditor.exe!UnknownFunction []
[2024.01.23-21.03.33:397][319]LogOutputDevice: Error: [Callstack] 0x00007ffb1da27344 KERNEL32.DLL!UnknownFunction []
[2024.01.23-21.03.33:400][319]LogOutputDevice: Error: [Callstack] 0x00007ffb1db626b1 ntdll.dll!UnknownFunction []

And then it still connects successfully after this error.

Fortunately, this isn’t breaking anything, but I am trying to set up some unit tests and despite the connection being successful, my automation test is failing due to this error being present.

Looking in my code, I’m not really doing any changes to arrays while iterating through them. And that call trace seems to be pointing at Unreal engine code, so it’s really difficult to understand what is causing this.

Any help is greatly appreciated.

Got a little more information now after downloading debug symbols:

LogOutputDevice: Error: === Handled ensure: ===
LogOutputDevice: Error: Ensure condition failed: CurrentNum == InitialNum  [File:D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 256] 
LogOutputDevice: Error: Array has changed during ranged-for iteration!
LogOutputDevice: Error: Stack: 
LogOutputDevice: Error: [Callstack] 0x00007ff9c984b366 UnrealEditor-WebSockets.dll!FLwsWebSocketsManager::GameThreadTick() [D:\build\++UE5\Sync\Engine\Source\Runtime\Online\WebSockets\Private\Lws\LwsWebSocketsManager.cpp:428]
LogOutputDevice: Error: [Callstack] 0x00007ff9c984a4bd UnrealEditor-WebSockets.dll!TBaseRawMethodDelegateInstance<0,FLwsWebSocketsManager,bool __cdecl(float),FDefaultDelegateUserPolicy>::Execute() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:518]
LogOutputDevice: Error: [Callstack] 0x00007ffa15216870 UnrealEditor-Core.dll!FTSTicker::FElement::Fire() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Private\Containers\Ticker.cpp:157]
LogOutputDevice: Error: [Callstack] 0x00007ffa1523541b UnrealEditor-Core.dll!FTSTicker::Tick() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Private\Containers\Ticker.cpp:110]
LogOutputDevice: Error: [Callstack] 0x00007ffa15201778 UnrealEditor-Core.dll!`FTSBackgroundableTicker::FTSBackgroundableTicker'::`2'::<lambda_1>::operator()() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Private\Containers\BackgroundableTicker.cpp:28]
LogOutputDevice: Error: [Callstack] 0x00007ffa1521508e UnrealEditor-Core.dll!TBaseFunctorDelegateInstance<bool __cdecl(float),FDefaultDelegateUserPolicy,TFunction<bool __cdecl(float)> >::Execute() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:863]
LogOutputDevice: Error: [Callstack] 0x00007ffa15216870 UnrealEditor-Core.dll!FTSTicker::FElement::Fire() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Private\Containers\Ticker.cpp:157]
LogOutputDevice: Error: [Callstack] 0x00007ffa1523541b UnrealEditor-Core.dll!FTSTicker::Tick() [D:\build\++UE5\Sync\Engine\Source\Runtime\Core\Private\Containers\Ticker.cpp:110]
LogOutputDevice: Error: [Callstack] 0x00007ff7a530cea9 UnrealEditor.exe!FEngineLoop::Tick() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:6073]
LogOutputDevice: Error: [Callstack] 0x00007ff7a532d28c UnrealEditor.exe!GuardedMain() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Launch.cpp:188]
LogOutputDevice: Error: [Callstack] 0x00007ff7a532d37a UnrealEditor.exe!GuardedMainWrapper() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:118]
LogOutputDevice: Error: [Callstack] 0x00007ff7a5330854 UnrealEditor.exe!LaunchWindowsStartup() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:258]
LogOutputDevice: Error: [Callstack] 0x00007ff7a5346984 UnrealEditor.exe!WinMain() [D:\build\++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:298]
LogOutputDevice: Error: [Callstack] 0x00007ff7a5349d7a UnrealEditor.exe!__scrt_common_main_seh() [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288]
LogOutputDevice: Error: [Callstack] 0x00007ffa8a347344 KERNEL32.DLL!UnknownFunction []
LogOutputDevice: Error: [Callstack] 0x00007ffa8a6026b1 ntdll.dll!UnknownFunction []
LogStats:                SubmitErrorReport -  0.000 s

None of the referenced source files in this stack trace point to any of my code… I don’t really understand what is actually happening here, but it points to this loop here in LwsWebSocketManager.cpp:

for (const FLwsWebSocketRef& Socket : Sockets)
	{
		Socket->GameThreadTick();
	}

And that is in the GameThreadTick() function. So because I am using 2 sockets, here’s what I’m assuming is happening:
I make my first websocket connection with no issue, and that would start looping that array in the websocket manager because it would not be empty anymore, but then when trying to make my second websocket connection, I initialize a new websocket, doing so adds a socket to that Sockets array possibly while it is looping, since it is on a tick function, and then produces this error. And while this doesn’t break anything, because on the next tick that new socket will init and function just fine, it still will produce an error in the console.

I’m assuming this can be reported as an Unreal Engine bug? Since this is in the LwsWebSocketsManager class of Unreal Engine?

1 Like

A bit late, but I have managed to replicate your findings.
The reason why the ensure is triggering is because you are creating the second Websocket in a scope of

Socket->GameThreadTick();

For example, if you have Websocket 1 and you are listening for messages using

WebSocket1->OnMessage().AddUObject(this, &ThisClass::HandleWebSocketMessage);

HandleWebSocketMessage is executed via Socket->GameThreadTick(); thefore in the for loop.

So if you were to receive a message “Please open websocket 2” on the “Websocket 1” and create new WS in HandleWebSocketMessage, it would be in the for loop iteration and you would add a second Websocket to the Sockets array mid-iterating.

Since it’s possible to only add during the iteration, there is nothing technically wrong with it, but I have submitted a Pull Request to fix ensure being triggered. https://github.com/EpicGames/UnrealEngine/pull/11934

That makes a lot of sense, thanks for the reply and the pull request, I’m glad it was so simple to fix, I hadn’t thought about just making it into a simple for loop, brilliant! Really appreciate it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.