@@ -313,14 +313,14 @@ static void nio_write(hio_t* io) {
313
313
return ;
314
314
}
315
315
offset_buf_t * pbuf = write_queue_front (& io -> write_queue );
316
- char * buf = pbuf -> base + pbuf -> offset ;
316
+ char * base = pbuf -> base ;
317
+ char * buf = base + pbuf -> offset ;
317
318
int len = pbuf -> len - pbuf -> offset ;
318
319
nwrite = __nio_write (io , buf , len );
319
320
// printd("write retval=%d\n", nwrite);
320
321
if (nwrite < 0 ) {
321
322
err = socket_errno ();
322
323
if (err == EAGAIN ) {
323
- //goto write_done;
324
324
hrecursive_mutex_unlock (& io -> write_mutex );
325
325
return ;
326
326
} else {
@@ -336,10 +336,14 @@ static void nio_write(hio_t* io) {
336
336
io -> write_bufsize -= nwrite ;
337
337
__write_cb (io , buf , nwrite );
338
338
if (nwrite == len ) {
339
- HV_FREE (pbuf -> base );
339
+ // NOTE: after write_cb, pbuf maybe invalid.
340
+ // HV_FREE(pbuf->base);
341
+ HV_FREE (base );
340
342
write_queue_pop_front (& io -> write_queue );
341
- // write next
342
- goto write ;
343
+ if (!io -> closed ) {
344
+ // write continue
345
+ goto write ;
346
+ }
343
347
}
344
348
hrecursive_mutex_unlock (& io -> write_mutex );
345
349
return ;
@@ -488,10 +492,10 @@ int hio_write (hio_t* io, const void* buf, size_t len) {
488
492
}
489
493
}
490
494
write_done :
495
+ hrecursive_mutex_unlock (& io -> write_mutex );
491
496
if (nwrite > 0 ) {
492
497
__write_cb (io , buf , nwrite );
493
498
}
494
- hrecursive_mutex_unlock (& io -> write_mutex );
495
499
return nwrite ;
496
500
write_error :
497
501
disconnect :
@@ -510,18 +514,24 @@ int hio_close (hio_t* io) {
510
514
if (hv_gettid () != io -> loop -> tid ) {
511
515
return hio_close_async (io );
512
516
}
517
+
513
518
hrecursive_mutex_lock (& io -> write_mutex );
514
- if (! write_queue_empty ( & io -> write_queue ) && io -> error == 0 && io -> close == 0 ) {
519
+ if (io -> closed ) {
515
520
hrecursive_mutex_unlock (& io -> write_mutex );
521
+ return 0 ;
522
+ }
523
+ if (!write_queue_empty (& io -> write_queue ) && io -> error == 0 && io -> close == 0 ) {
516
524
io -> close = 1 ;
525
+ hrecursive_mutex_unlock (& io -> write_mutex );
517
526
hlogw ("write_queue not empty, close later." );
518
527
int timeout_ms = io -> close_timeout ? io -> close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT ;
519
528
io -> close_timer = htimer_add (io -> loop , __close_timeout_cb , timeout_ms , 1 );
520
529
io -> close_timer -> privdata = io ;
521
530
return 0 ;
522
531
}
523
-
524
532
io -> closed = 1 ;
533
+ hrecursive_mutex_unlock (& io -> write_mutex );
534
+
525
535
hio_done (io );
526
536
__close_cb (io );
527
537
if (io -> ssl ) {
@@ -531,7 +541,6 @@ int hio_close (hio_t* io) {
531
541
if (io -> io_type & HIO_TYPE_SOCKET ) {
532
542
closesocket (io -> fd );
533
543
}
534
- hrecursive_mutex_unlock (& io -> write_mutex );
535
544
return 0 ;
536
545
}
537
546
#endif
0 commit comments