Repository: microsoft/terminal
File: src/cascadia/TerminalConnection/ConptyConnection.cpp (C++)

Code before:
    DWORD ConptyConnection::_OutputThread()
    {
        // Keep us alive until the output thread terminates; the destructor
        // won't wait for us, and the known exit points _do_.
        auto strongThis{ get_strong() };

        // process the data of the output pipe in a loop
        while (true)
        {
            DWORD read{};

            const auto readFail{ !ReadFile(_outPipe.get(), _buffer.data(), gsl::narrow_cast<DWORD>(_buffer.size()), &read, nullptr) };

            // When we call CancelSynchronousIo() in Close() this is the branch that's taken and gets us out of here.
            if (_isStateAtOrBeyond(ConnectionState::Closing))
            {
                return 0;
            }

            if (readFail) // reading failed (we must check this first, because read will also be 0.)
            {
                // EXIT POINT
                const auto lastError = GetLastError();
                if (lastError == ERROR_BROKEN_PIPE)
                {
                    _ClientTerminated();
                    return S_OK;
                }
                else
                {
                    _indicateExitWithStatus(HRESULT_FROM_WIN32(lastError)); // print a message
                    _transitionToState(ConnectionState::Failed);
                    return gsl::narrow_cast<DWORD>(HRESULT_FROM_WIN32(lastError));
                }
            }

            const auto result{ til::u8u16(std::string_view{ _buffer.data(), read }, _u16Str, _u8State) };
            if (FAILED(result))
            {
                // EXIT POINT
                _indicateExitWithStatus(result); // print a message
                _transitionToState(ConnectionState::Failed);
                return gsl::narrow_cast<DWORD>(result);
            }

            if (_u16Str.empty())
            {
                return 0;
            }

            if (!_receivedFirstByte)

Review comment (suggestion):
it might be best for us to rename `_ClientTerminated` to `_LastConPtyClientDisconnected` or something

Code after:
        {
            DWORD read{};

            const auto readFail{ !ReadFile(_outPipe.get(), _buffer.data(), gsl::narrow_cast<DWORD>(_buffer.size()), &read, nullptr) };

            // When we call CancelSynchronousIo() in Close() this is the branch that's taken and gets us out of here.
            if (_isStateAtOrBeyond(ConnectionState::Closing))
            {
                return 0;
            }

            if (readFail) // reading failed (we must check this first, because read will also be 0.)
            {
                // EXIT POINT
                const auto lastError = GetLastError();
                if (lastError == ERROR_BROKEN_PIPE)
                {
                    _LastConPtyClientDisconnected();
                    return S_OK;
                }
                else
                {
                    _indicateExitWithStatus(HRESULT_FROM_WIN32(lastError)); // print a message
                    _transitionToState(ConnectionState::Failed);
                    return gsl::narrow_cast<DWORD>(HRESULT_FROM_WIN32(lastError));
                }
            }

            const auto result{ til::u8u16(std::string_view{ _buffer.data(), read }, _u16Str, _u8State) };
            if (FAILED(result))
            {
                // EXIT POINT
                _indicateExitWithStatus(result); // print a message
                _transitionToState(ConnectionState::Failed);
                return gsl::narrow_cast<DWORD>(result);
            }

            if (_u16Str.empty())
            {
                return 0;
            }

            if (!_receivedFirstByte)
            {
                const auto now = std::chrono::high_resolution_clock::now();
                const std::chrono::duration<double> delta = now - _startTime;

#pragma warning(suppress : 26477 26485 26494 26482 26446) // We don't control TraceLoggingWrite
                TraceLoggingWrite(g_hTerminalConnectionProvider,
                                  "ReceivedFirstByte",
                                  TraceLoggingDescription("An event emitted when the connection receives the first byte"),