Mikael
Mikael

Reputation: 1319

Chilkat php imap Connect hangs forever

I use Chilkat php extension. And I have noticed that chilkat method Connect hangs forever. put_ReadTimeout() and put_ConnectTimeout() doesn't fix it.

Also I use it via ssh. The code is simple like this:

// $this->imap is a CKImap class

$this->imap->put_ConnectTimeout(10);
$this->imap->put_Ssl(true);
$this->imap->put_PeekMode(true);
$this->imap->put_Utf8(true);
$this->imap->put_VerboseLogging(true);
$this->imap->put_DebugLogFilePath('/var/www/logs/log_chilkat.txt');
$this->imap->SshOpenTunnel($proxy->ip, $proxy->port);
$this->imap->SshAuthenticatePw($proxy->username,$proxy->password);

// At this point it hangs forever
$this->imap->Connect($account->imap_server);
$this->imap->login($account->email, $account->password);

I have missed any useless condition and other code. It is working code and sometimes it hangs at Connect line. Maybe problem is in SSH, maybe SSH hangs ?

How to solve it ? I have not found way to solve with chilkat functions. Also I don't want to use signals to stop fetch function execution.

I have figured it out. I have done a lot tests and it hangs at the next line: $this->imap->Connect($account->imap_server);

Chilkat verbose logs are the following:

SshOpenTunnel:
    DllDate: May 11 2019
    ChilkatVersion: 9.5.0.78
    UnlockPrefix: *******
    Architecture: Little Endian; 64-bit
    Language: Linux PHP
    VerboseLogging: 1
    sshTunnel:
        tunnelIdleTimeoutMs: 30000
        tunnelConnectTimeoutMs: 10000
        sshConnect:
            connectSocket:
                domainOrIpAddress: ******
                port: 22
                connectTimeoutMs: 10000
                connect_ipv6_or_ipv4:
                    This is an IPV4 numeric address.
                    Domain to IP address resolution not needed.
                    getAddressInfo:
                        (leaveContext)
                    findIpAddrInfo:
                        (leaveContext)
                    connecting to IPV4 address...
                    ipAddress: *****
                    createSocket:
                        Setting SO_SNDBUF size
                        sendBufSize: 262144
                        Setting SO_RCVBUF size
                        recvBufSize: 4194304
                        (leaveContext)
                    connect:
                        Waiting for the connect to complete...
                        connectTimeoutMs: 10000
                        ck_getsockname_ipv4:
                            (leaveContext)
                        myIP: *****
                        myPort: 48882
                        socket connect successful.
                        (leaveContext 261ms)
                    (leaveContext 261ms)
                (leaveContext 261ms)
            Established TCP/IP connection with SSH server
            Turning on TCP_NODELAY.
            (leaveContext 262ms)
        sshSetupConnection:
            clientIdentifier: SSH-2.0-PuTTY_Release_0.70
            Sending client identifier...
            Done sending client identifier.
            Reading server version...
            initialDataFromSshServer: SSH-2.0-OpenSSH_4.3

            serverVersion: SSH-2.0-OpenSSH_4.3
            build_kexInit:
                preferRsaHostKeyAlgorithm: 1
                (leaveContext)
            KeyExchangeAlgs:
                algorithm: diffie-hellman-group-exchange-sha1
                algorithm: diffie-hellman-group14-sha1
                algorithm: diffie-hellman-group1-sha1
                (leaveContext)
            HostKeyAlgs:
                algorithm: ssh-dss
                algorithm: ssh-rsa
                (leaveContext 1ms)
            EncCS:
                algorithm: aes128-ctr
                algorithm: aes192-ctr
                algorithm: aes256-ctr
                algorithm: arcfour256
                algorithm: arcfour128
                algorithm: aes128-cbc
                algorithm: 3des-cbc
                algorithm: blowfish-cbc
                algorithm: cast128-cbc
                algorithm: aes192-cbc
                algorithm: aes256-cbc
                algorithm: arcfour
                algorithm: [email protected]
                (leaveContext)
            EncSC:
                algorithm: aes128-ctr
                algorithm: aes192-ctr
                algorithm: aes256-ctr
                algorithm: arcfour256
                algorithm: arcfour128
                algorithm: aes128-cbc
                algorithm: 3des-cbc
                algorithm: blowfish-cbc
                algorithm: cast128-cbc
                algorithm: aes192-cbc
                algorithm: aes256-cbc
                algorithm: arcfour
                algorithm: [email protected]
                (leaveContext)
            MacCS:
                algorithm: hmac-md5
                algorithm: hmac-sha1
                algorithm: hmac-ripemd160
                algorithm: [email protected]
                algorithm: hmac-sha1-96
                algorithm: hmac-md5-96
                (leaveContext)
            MacSC:
                algorithm: hmac-md5
                algorithm: hmac-sha1
                algorithm: hmac-ripemd160
                algorithm: [email protected]
                algorithm: hmac-sha1-96
                algorithm: hmac-md5-96
                (leaveContext)
            CompCS:
                algorithm: none
                algorithm: [email protected]
                algorithm: zlib
                (leaveContext)
            CompSC:
                algorithm: none
                algorithm: [email protected]
                algorithm: zlib
                (leaveContext)
            LangCS:
                (leaveContext)
            LangSC:
                (leaveContext)
            ChosenIncomingEncryption: aes128-ctr
            ChosenOutgoingEncryptoin: aes128-ctr
            ChosenIncomingMac: hmac-sha1
            ChosenOutgoingMac: hmac-sha1
            ChosenIncomingCompression: zlib
            ChosenOutgoingCompression: zlib
            ChosenKexAlgorithm: diffie-hellman-group1-sha1
            choose_hostkey_algorithm:
                (leaveContext)
            ChosenHostKeyAlgorithm: ssh-rsa
            numBits: 128
            pbits: 1024
            Using Oakley Group 2.
            sendDhInit:
                create_E:
                    (leaveContext 7ms)
                Sent: SSH_MSG_KEXDH_INIT
                (leaveContext 7ms)
            computeExchangeHash:
                dhReplyMsgType: 31
                serverVersion: [SSH-2.0-OpenSSH_4.3]
                Using SHA-1 for Key Exchange Hash
                (leaveContext)
            verifyHostKey:
                rsaHostKeyVerify:
                    alg: ssh-rsa
                    hostKeyNumBits: 2048
                    verifyHashSsh:
                        Pkcs1_5_decode:
                            (leaveContext)
                        (leaveContext)
                    (leaveContext 1ms)
                RSA host key signature verification success.
                (leaveContext 1ms)
            calculateKey:
                (leaveContext)
            calculateKey:
                (leaveContext)
            calculateKey:
                (leaveContext)
            calculateKey:
                (leaveContext)
            calculateKey:
                (leaveContext)
            calculateKey:
                (leaveContext)
            Sending newkeys to server...
            Expecting newkeys from server...
            SSH Key Exchange Success.
            installNewKeys:
                m_isRekey: 0
                Outgoing compression is now zlib.
                Incoming compression is now zlib.
                Outgoing encryption is now AES 128 CTR
                initCrypt_aes2:
                    (leaveContext)
                initCrypt_aes2:
                    (leaveContext)
                (leaveContext)
            (leaveContext 934ms)
        (leaveContext 1197ms)
    Success.
    (leaveContext 1197ms)
SshAuthenticatePw:
    DllDate: May 11 2019
    ChilkatVersion: 9.5.0.78
    UnlockPrefix: *******
    Architecture: Little Endian; 64-bit
    Language: Linux PHP
    VerboseLogging: 1
    sshAuthenticatePw:
        sshAuthenticatePw:
            requestUserAuthService:
                sendServiceRequest:
                    svcName: ssh-userauth
                    SentServiceReq: ssh-userauth
                    (leaveContext)
                ssh-userauth service accepted.
                (leaveContext 735ms)
            sendUserAuthQuery:
                (leaveContext)
            AuthMethods: publickey,password
            passwordAuth:
                Sent login/password
                Password authentication successful.
                (leaveContext 593ms)
            (leaveContext 1762ms)
        (leaveContext 1762ms)
    Success.
    (leaveContext 1762ms)
Connect_Imap:
    DllDate: May 11 2019
    ChilkatVersion: 9.5.0.78
    UnlockPrefix: *****
    Architecture: Little Endian; 64-bit
    Language: Linux PHP
    VerboseLogging: 1
    connectInner:
        connectToImapServer:
            hostname: ******
            port: 993
            Closing the SSH channel, if it exists.
            sshCloseChannel:
                (leaveContext)
            socket2Connect:
                connect2:
                    hostname: ******
                    port: 993
                    ssl: True
                    sshOpenChannel:
                        Opening new SSH channel within SSH tunnel.
                        sshTransportOpenChannel:
                            channelType: direct-tcpip
                            clientChannel: 4815
                            clientInitialWindowSize: 2097152
                            clientMaxPacketSize: 32768
                            directTcpHost: ******
                            directTcpPort: 993
                            ck_getsockname_ipv4:
                                (leaveContext)
                            originatorIP: ****
                            originatorPort: 48882
                            Sent open channel request
                            ClientChannelNum: 4815
                            ServerChannelNum: 0
                            ServerInitialWindowSize: 2097152
                            serverMaxPacketSize: 32768
                            (leaveContext 1324ms)
                        [SSH] Direct TCP/IP channel successfully opened.
                        sshChannelNum: 4815
                        (leaveContext 1324ms)
                    Setting up SSL/TLS to run through an SSH tunnel...
                    Clearing TLS client certificates.
                    clientHandshake:
                        The client cert chain is NULL.
                        cacheClientCerts:
                            Cached TLS client certificates.
                            Client cert chain is NULL.
                            (leaveContext)
                        clientHandshake2:
                            createRandom:
                                (leaveContext)
                            sendClientHello:
                                TlsClientHello_buildMessage:
                                    (leaveContext)
                                sendHandshakeMessages:
                                    channelSendData2:
                                        (leaveContext)
                                    (leaveContext 1ms)
                                (leaveContext 1ms)
                            readHandshakeMessages:
                                processHandshakeRecord:
                                    processHandshakeMessage:
                                        processServerHello:
                                            negotiatedTlsVersion: TLS 1.2
                                            negotiatedCipherSuite: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
                                            minAcceptableRsaKeySize: 1024
                                            (leaveContext)
                                        (leaveContext)
                                    (leaveContext)
                                (leaveContext 3429ms)
                            readHandshakeMessages:
                                processHandshakeRecord:
                                    processHandshakeMessage:
                                        processIncomingCertificates:
                                            loadX509DerAlt:
                                                der_to_xml:
                                                    (leaveContext)
                                                (leaveContext)
                                            loadX509DerAlt:
                                                der_to_xml:
                                                    (leaveContext)
                                                (leaveContext)
                                            (leaveContext)
                                        (leaveContext)
                                    (leaveContext 1ms)
                                (leaveContext 1409ms)
                            handleServerCert:
                                readHandshakeMessages:
                                    processHandshakeRecord:
                                        processHandshakeMessage:
                                            processServerKeyExchange:
                                                processEcDheKx:
                                                    (leaveContext)
                                                (leaveContext)
                                            (leaveContext)
                                        (leaveContext)
                                    (leaveContext)
                                (leaveContext)
                            handleServerKeyExchange:
                                readHandshakeMessages:
                                    processHandshakeRecord:
                                        processHandshakeMessage:
                                            (leaveContext)
                                        (leaveContext)
                                    (leaveContext)
                                (leaveContext 1ms)
                            buildClientKeyExchange:
                                buildClientKeyExchangeECDHE:
                                    verifyServerKeyExchange:
                                        getServerCertPublicKey:
                                            x509_getPublicKey:
                                                (leaveContext)
                                            (leaveContext)
                                        loadAnyDer:
                                            DecodeToAsn:
                                                (leaveContext)
                                            loadAnyAsn:
                                                loadRsaPkcs1Asn:
                                                    (leaveContext)
                                                (leaveContext)
                                            (leaveContext)
                                        composeVerifyData:
                                            (leaveContext)
                                        verifyHash:
                                            keyType: Public
                                            hashInSize: 32
                                            padding: PKCS v1.5
                                            Pkcs1_5_decode:
                                                (leaveContext)
                                            HashOid: 2.16.840.1.101.3.4.2.1
                                            (leaveContext 1ms)
                                        (leaveContext 1ms)
                                    Verified server key exchange.
                                    generateNewKey_ecc:
                                        loadCurveByName:
                                            name: secp256r1
                                            (leaveContext)
                                        genPubKey:
                                            pointMultiply:
                                                (leaveContext 6ms)
                                            (leaveContext 6ms)
                                        (leaveContext 6ms)
                                    exportEccPoint:
                                        (leaveContext)
                                    loadSshPubKey:
                                        loadCurveByName:
                                            name: secp256r1
                                            (leaveContext)
                                        loadEccPoint:
                                            (leaveContext)
                                        (leaveContext)
                                    sharedSecret:
                                        pointMultiply:
                                            (leaveContext 6ms)
                                        (leaveContext 6ms)
                                    (leaveContext 13ms)
                                (leaveContext 13ms)
                            sendClientKeyExchange:
                                sendHandshakeMessages:
                                    channelSendData2:
                                        (leaveContext)
                                    (leaveContext)
                                (leaveContext)
                            sendChangeCipherSpec:
                                channelSendData2:
                                    (leaveContext 1ms)
                                (leaveContext 1ms)
                            derive_keys:
                                initCrypt_aes2:
                                    (leaveContext)
                                initCrypt_aes2:
                                    (leaveContext)
                                (leaveContext)
                            buildFinished:
                                calc_finished:
                                    (leaveContext)
                                (leaveContext)
                            sendHandshakeMessages:
                                channelSendData2:
                                    (leaveContext)
                                (leaveContext)
                            readHandshakeMessages:
                                Returning because of channel EOF
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                Received EOF on SSH channel.
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.
                                Received CHANNEL REQUEST message
                                channelNum: 4815
                                requestType: [email protected]
                                wantReply: 1
                                Received EOF on SSH channel.

Logs just ends at Received EOF on SSH channel. As you see I use last Chilkat version. My environment is Ubuntu server 18.04 64bit. PHP 7.2.2 ZTS. Also the same problem I had on Windows.

!!! Also important notice, the application is running in multithreading, 45 threads. Each thread has own CkImap object and performs connecting to imap via ssh. And sometimes some thread hangs at Connect to imap point.

How to solve this hanging ?

I have found some method put_AbortCurrent. May I use it to abort Connect method? In my opinion the next code has to abort Connect after connect timeout:

        $this->imap->put_AbortCurrent(true);
        $connectedToImap = $this->imap->Connect($account->imap_server);
        $this->imap->put_AbortCurrent(false);

Upvotes: 1

Views: 397

Answers (1)

Chilkat Software
Chilkat Software

Reputation: 1624

Use VerboseLogging and DebugLogFilePath to find out what happened. Reproduce the hang w/ these properties set. Then look at the log file to see where the hang occurred (see the sample code below).

Also, test using the latest version of Chilkat. If you are using an old version, perhaps something was fixed long ago. Release notes are available at: http://cknotes.com/chilkat-v9-5-0-78-release-notes/


    $imap = new CkImap();

    $imap->put_VerboseLogging(true);
    $imap->put_DebugLogFilePath('someDir/imapDebugLog.txt');
    $imap->put_ReadTimeout(10);

    // ...

    // Some UID...
    $msgId = 123;
    $bUid = true;
    $strMime = $imap->fetchSingleAsMime($msgId,$bUid);
    if ($imap->get_LastMethodSuccess() != true) {
        print $imap->lastErrorText() . "\n";
        exit;
    }

    print $strMime . "\n";

Upvotes: 1

Related Questions