Tedious: ConnectionError: Failed to connect to {{host}}:1433 - Cannot call write after a stream was destroyed

Created on 29 Jul 2019  ·  14Comments  ·  Source: tediousjs/tedious

Error while connecting to Database (Sql Server 2008 R2)

I am using "mssql": "^5.1.0"

I am able to connect to localhost and SQL Server 2017

[Nest] 29632 - 07/29/2019, 9:34 PM [TypeOrmModule] Unable to connect to the database. Retrying (1)... +876ms
ConnectionError: Failed to connect to {{host}}:1433 - Cannot call write after a stream was destroyed
at Connection. (nrwlnode_modulesmssqllibtedious.js:244:17)
at Object.onceWrapper (events.js:288:20)
at Connection.emit (events.js:200:13)
at Connection.socketError (nrwlnode_modulestediouslibconnection.js:1098:12)
at Socket. (nrwlnode_modulestediouslibconnection.js:971:14)
at Socket.emit (events.js:205:15)
at errorOrDestroy (internal/streams/destroy.js:107:12)
at onwriteError (_stream_writable.js:432:5)
at onwrite (_stream_writable.js:459:5)
at doWrite (_stream_writable.js:413:11)

Kindly suggest some solution :)

Idle

Most helpful comment

forget it! encrypt, not encrypted

All 14 comments

Hi @bj9215,

What version of tedious are you using? Mssql package.json is using an outdated version of tedious (current tedious version is v6.2.0). Try updating that and see if it helps? :)

Which version of Node.js are you running on? Do you have encryption enabled?

As you're trying to connect to an older version of SQL Server, you most likely need to tweak the encryption settings to use an older version of TLS and use a ciphersuite that is supported on both SQL Server 2008 and the Node.js version you are using.

@bj9215 Are you still having this issue?

I have a similar proplem wit "tedious": "6.6.2",

const config = {
    server: 'local-sql-host',
    authentication: {
        type: 'default',
        options: {
            userName: process.env.AUTH_LOCAL_USR,
            password: process.env.AUTH_LOCAL_PWD,
        },
    },
    options: {
        database: 'DATABASE_NAME',
        encrypted: false,
    },
}
debug connected to {local-sql-host}:1433
debug State change: Connecting -> SentPrelogin
debug State change: SentPrelogin -> SentTLSSSLNegotiation
debug Failed to connect to {local-sql-host}:1433 - Cannot call write after a stream was destroyed
connect $db> test.(): ConnectionError: Failed to connect to {local-sql-host}:1433 - Cannot call write after a stream was de
stroyed
    at ConnectionError (C:\workspace\api-v3\dist\webpack:\node_modules\tedious\lib\errors.js:13:1)
    at Connection.socketError (C:\workspace\api-v3\dist\webpack:\node_modules\tedious\lib\connection.js:1258:1)
    at Socket.<anonymous> (C:\workspace\api-v3\dist\webpack:\node_modules\tedious\lib\connection.js:1093:1)
    at Socket.emit (events.js:215:7)
    at errorOrDestroy (internal/streams/destroy.js:108:12)
    at onwriteError (_stream_writable.js:446:5)
    at onwrite (_stream_writable.js:473:5)
    at doWrite (_stream_writable.js:427:11)
    at writeOrBuffer (_stream_writable.js:415:5)
    at Socket.Writable.write (_stream_writable.js:305:11) {
  message: 'Failed to connect to {local-sql-host}:1433 - Cannot call write after a stream was destroyed',
  code: 'ESOCKET'
}
debug State change: SentTLSSSLNegotiation -> Final
debug Connection lost - 99404:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\o
penssl\openssl\ssl\statem\statem_lib.c:1929:

ConnectionError: Connection lost - 99404:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:c
:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1929:

    at ConnectionError (C:\workspace\api-v3\dist\webpack:\node_modules\tedious\lib\errors.js:13:1)
    at Connection.socketError (C:\workspace\api-v3\dist\webpack:\node_modules\tedious\lib\connection.js:1262:1)
    at Socket.<anonymous> (C:\workspace\api-v3\dist\webpack:\node_modules\tedious\lib\connection.js:1093:1)
    at Socket.emit (events.js:210:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
[ERROR] 12:23:11 ConnectionError

forget it! encrypt, not encrypted

@bj9215 Closing for now. Feel free to re-open if you're still having this issue!

One million of thanks!!

I ran into the same issue. Looks like node-mssql changed the encrypt default from false to true in this PR:
https://github.com/tediousjs/node-mssql/pull/977

@ataft's comment helped me solve my issue as well - setting "encrypt" to false fixed my issue.

@crazyx13th Thanks, this helped me

forget it! encrypt, not ~encrypted~

This is what if trying to fix on 1 week.
Error message is very unintelligible.

Which version of Node.js are you running on? Do you have encryption enabled?

As you're trying to connect to an older version of SQL Server, you most likely need to tweak the encryption settings to use an older version of TLS and use a ciphersuite that is supported on both SQL Server 2008 and the Node.js version you are using.

Thanks. This solved the issue. i just added _--tls-min-v1.0_ flag

Hola tengo el mismo error y ya trate de hacer lo que han comentado anterior mente y me sigue saliendo el mismo error
ConnectionError: Failed to connect to {host}:1433 - Cannot call write after a stream was destroyed

Esta es mi cadena de conexión.

const config = {
user: 'user',
password: 'password',
server: 'host',
database: 'name-dataBase',
pool:{
max: 230,
min: 0
}
};

I just used this parameter in database connection options, options: { encrypt: false }

Was this page helpful?
0 / 5 - 0 ratings