@@ -9,35 +9,46 @@ const net = require('net');
99const key = fs . readFileSync ( common . fixturesDir + '/keys/agent2-key.pem' ) ;
1010const cert = fs . readFileSync ( common . fixturesDir + '/keys/agent2-cert.pem' ) ;
1111
12- const T = 100 ;
13-
12+ let tlsSocket ;
1413// tls server
1514const tlsServer = tls . createServer ( { cert, key } , ( socket ) => {
16- setTimeout ( ( ) => {
17- socket . on ( 'error' , ( error ) => {
18- assert . strictEqual ( error . code , 'EINVAL' ) ;
19- tlsServer . close ( ) ;
20- netServer . close ( ) ;
21- } ) ;
22- socket . write ( 'bar' ) ;
23- } , T * 2 ) ;
15+ tlsSocket = socket ;
16+ socket . on ( 'error' , common . mustCall ( ( error ) => {
17+ assert . strictEqual ( error . code , 'EINVAL' ) ;
18+ tlsServer . close ( ) ;
19+ netServer . close ( ) ;
20+ } ) ) ;
2421} ) ;
2522
23+ let netSocket ;
2624// plain tcp server
2725const netServer = net . createServer ( ( socket ) => {
28- // if client wants to use tls
26+ // if client wants to use tls
2927 tlsServer . emit ( 'connection' , socket ) ;
3028
31- socket . setTimeout ( T , ( ) => {
32- // this breaks if TLSSocket is already managing the socket:
33- socket . destroy ( ) ;
34- } ) ;
29+ netSocket = socket ;
3530} ) . listen ( 0 , common . mustCall ( function ( ) {
36-
3731 // connect client
3832 tls . connect ( {
3933 host : 'localhost' ,
4034 port : this . address ( ) . port ,
4135 rejectUnauthorized : false
42- } ) . write ( 'foo' ) ;
36+ } ) . write ( 'foo' , 'utf8' , common . mustCall ( ( ) => {
37+ assert ( netSocket ) ;
38+ netSocket . setTimeout ( 1 , common . mustCall ( ( ) => {
39+ assert ( tlsSocket ) ;
40+ // this breaks if TLSSocket is already managing the socket:
41+ netSocket . destroy ( ) ;
42+ const interval = setInterval ( ( ) => {
43+ // Checking this way allows us to do the write at a time that causes a
44+ // segmentation fault (not always, but often) in Node.js 7.7.3 and
45+ // earlier. If we instead, for example, wait on the `close` event, then
46+ // it will not segmentation fault, which is what this test is all about.
47+ if ( tlsSocket . _handle . _parent . bytesRead === 0 ) {
48+ tlsSocket . write ( 'bar' ) ;
49+ clearInterval ( interval ) ;
50+ }
51+ } , 1 ) ;
52+ } ) ) ;
53+ } ) ) ;
4354} ) ) ;
0 commit comments