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