Repository: microsoft/terminal
File: src/host/srvinit.cpp (C++)

Code before:
        ReceiveMsg._pApiRoutines = &globals.api;
        ReceiveMsg._pDeviceComm = globals.pDeviceComm;
        IoSorter::ServiceIoOperation(&ReceiveMsg, &ReplyMsg);
    }

    bool fShouldExit = false;
    while (!fShouldExit)
    {
        if (ReplyMsg != nullptr)
        {
            LOG_IF_FAILED(ReplyMsg->ReleaseMessageBuffers());
        }

        // TODO: 9115192 correct mixed NTSTATUS/HRESULT
        HRESULT hr = ServiceLocator::LocateGlobals().pDeviceComm->ReadIo(ReplyMsg, &ReceiveMsg);
        if (FAILED(hr))
        {
            if (hr == HRESULT_FROM_WIN32(ERROR_PIPE_NOT_CONNECTED))
            {
                fShouldExit = true;

                // This will not return. Terminate immediately when disconnected.
                ServiceLocator::RundownAndExit(STATUS_SUCCESS);
            }
            RIPMSG1(RIP_WARNING, "DeviceIoControl failed with Result 0x%x", hr);
            ReplyMsg = nullptr;
            continue;
        }

        IoSorter::ServiceIoOperation(&ReceiveMsg, &ReplyMsg);
    }

    return 0;
}


Review comment (suggestion):
Meh, can we just patch `capturedMessage` directly and hand it off as a pointer instead of copying it?

```suggestion
        IoSorter::ServiceIoOperation(capturedMessage.get(), &ReplyMsg);
```

Code after:
// - This routine is the main one in the console server IO thread.
// - It reads IO requests submitted by clients through the driver, services and completes them in a loop.
// Arguments:
// - lpParameter - PCONSOLE_API_MSG being handed off to us from the previous I/O.
// Return Value:
// - This routine never returns. The process exits when no more references or clients exist.
DWORD WINAPI ConsoleIoThread(LPVOID lpParameter)
{
    auto& globals = ServiceLocator::LocateGlobals();

    CONSOLE_API_MSG ReceiveMsg;
    ReceiveMsg._pApiRoutines = &globals.api;
    ReceiveMsg._pDeviceComm = globals.pDeviceComm;
    PCONSOLE_API_MSG ReplyMsg = nullptr;

    // If we were given a message on startup, process that in our context and then continue with the IO loop normally.
    if (lpParameter)
    {
        // Capture the incoming lpParameter into a unique_ptr so we can appropriately
        // free the heap memory when we're done getting the important bits out of it below.
        std::unique_ptr<CONSOLE_API_MSG> capturedMessage{ static_cast<PCONSOLE_API_MSG>(lpParameter) };

        ReceiveMsg = *capturedMessage.get();
        ReceiveMsg._pApiRoutines = &globals.api;
        ReceiveMsg._pDeviceComm = globals.pDeviceComm;
        IoSorter::ServiceIoOperation(&ReceiveMsg, &ReplyMsg);
    }

    bool fShouldExit = false;
    while (!fShouldExit)
    {
        if (ReplyMsg != nullptr)
        {
            LOG_IF_FAILED(ReplyMsg->ReleaseMessageBuffers());
        }

        // TODO: 9115192 correct mixed NTSTATUS/HRESULT
        HRESULT hr = ServiceLocator::LocateGlobals().pDeviceComm->ReadIo(ReplyMsg, &ReceiveMsg);
        if (FAILED(hr))
        {
            if (hr == HRESULT_FROM_WIN32(ERROR_PIPE_NOT_CONNECTED))
            {
                fShouldExit = true;

                // This will not return. Terminate immediately when disconnected.
                ServiceLocator::RundownAndExit(STATUS_SUCCESS);
            }
            RIPMSG1(RIP_WARNING, "DeviceIoControl failed with Result 0x%x", hr);
            ReplyMsg = nullptr;
            continue;
        }