diff --git a/composer.json b/composer.json index ecbe17d..52c166c 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "expensify/bedrock-php", "description": "Bedrock PHP Library", "type": "library", - "version": "2.1.2", + "version": "2.1.3", "authors": [ { "name": "Expensify", diff --git a/src/Client.php b/src/Client.php index 8e4a641..5be943f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -552,7 +552,29 @@ private function sendRawRequest(string $host, int $port, string $rawRequest) @socket_connect($this->socket, $host, $port); $socketErrorCode = socket_last_error($this->socket); if ($socketErrorCode === 115) { - $this->logger->info('Bedrock\Client - socket_connect returned error 115, continuing.'); + $this->logger->info('Bedrock\Client - socket_connect returned EINPROGRESS, waiting for connection to complete'); + + // Use select to wait for the socket to become writable (indicates connection completion) + $write = [$this->socket]; + $read = null; + $except = null; + + $selectResult = socket_select($read, $write, $except, $this->connectionTimeout, $this->connectionTimeoutMicroseconds); + if ($selectResult === false) { + $socketError = socket_strerror(socket_last_error()); + throw new ConnectionFailure("Failed to select on socket for host $host:$port. Error: $socketError"); + } elseif ($selectResult === 0) { + throw new ConnectionFailure("Connection timeout while waiting for EINPROGRESS completion for host $host:$port"); + } + + // Check if connection completed successfully + $connectionError = socket_get_option($this->socket, SOL_SOCKET, SO_ERROR); + if ($connectionError !== 0) { + $socketError = socket_strerror($connectionError); + throw new ConnectionFailure("Connection failed after EINPROGRESS for host $host:$port. Error: $connectionError $socketError"); + } + + $this->logger->info('Bedrock\Client - EINPROGRESS connection completed successfully'); } elseif ($socketErrorCode) { $socketError = socket_strerror($socketErrorCode); throw new ConnectionFailure("Could not connect to Bedrock host $host:$port. Error: $socketErrorCode $socketError");