HttpServer Module doesn't seem to work with multiple instances

->UE4.26 (Engine Source Release)
->Win64 (Windows10)

Given this code using HttpServer module:


bool FPGHTTPServer::Start(int Port)
{
bool succeed = false;
ListenPort = Port;
auto HttpServerModule = &FHttpServerModule::Get();
TSharedPtr<IHttpRouter> HttpRouter;
do
{
HttpRouter = HttpServerModule->GetHttpRouter(ListenPort);
}
while (!HttpRouter.IsValid() && ListenPort++ < 60000);

if(HttpRouter.IsValid())
{
FString Route = "/health";
// One point to note here is that the bottom layer does not support different request methods with the same http path configuration
auto Handler = HttpRouter->BindRoute(FHttpPath(Route), EHttpServerRequestVerbs::VERB_GET, CreateHandler(&HealthCheck));
if(Handler)
{
HttpServerModule->StartAllListeners();
succeed = true;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Http Listener couldn't be created!"));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("HttpRouter couldn't be created!"));
}
return succeed;
}

and TWO server instances in TWO separate processes I have the next log report running the second instance:

At this code point:


HttpServerModule->StartAllListeners();

But programmer won’t be able to be notified about this issue at runtime cause HttpServerModule.cpp → StartAllListeners has this interface:


void FHttpServerModule::StartAllListeners()

The simplest solution I though is making this simple change in this method in HttpServerModule.cpp:


TSharedPtr<IHttpRouter> FHttpServerModule::GetHttpRouter(uint32 Port)
{
check(Singleton == this);

// We may already be listening on this port
TUniquePtr<FHttpListener>* ExistingListener = Listeners.Find(Port);
if (ExistingListener)
{
return ExistingListener->Get()->GetRouter();
}

// Otherwise create a new one
TUniquePtr<FHttpListener> NewListener = MakeUnique<FHttpListener>(Port);

// Try to start this listener now
**if (bHttpListenersEnabled)
{
NewListener->StartListening();
}**
const auto& NewListenerRef = Listeners.Add(Port, MoveTemp(NewListener));
return NewListenerRef->GetRouter();
}

for:


if (!NewListener->StartListening())
return nullptr;

Consequently, I can know If something went wrong and I can try it again using other port.

It might not be the best solution but works great for me. Now I could have as many UE4 Dedicated Servers with their HttpServer listening and it’s the feature that I need.

I hope this issue is fixed soon it doesn’t matter the way,
.