Cygwin: FIFO: close connect_evt handles as soon as possible

Keeping them open too long can cause an attempt to close them twice
after a fork or exec.
This commit is contained in:
Ken Brown 2019-04-20 11:31:08 -04:00
parent 24c56e5a2c
commit 252cd0ce2b
1 changed files with 10 additions and 7 deletions

View File

@ -363,6 +363,7 @@ fhandler_fifo::listen_client_thread ()
break; break;
} }
} }
HANDLE evt = NULL;
switch (status) switch (status)
{ {
case STATUS_SUCCESS: case STATUS_SUCCESS:
@ -371,6 +372,9 @@ fhandler_fifo::listen_client_thread ()
fc.state = fc_connected; fc.state = fc_connected;
nconnected++; nconnected++;
set_pipe_non_blocking (fc.fh->get_handle (), true); set_pipe_non_blocking (fc.fh->get_handle (), true);
evt = InterlockedExchangePointer (&fc.connect_evt, NULL);
if (evt)
CloseHandle (evt);
fifo_client_unlock (); fifo_client_unlock ();
break; break;
case STATUS_PIPE_LISTENING: case STATUS_PIPE_LISTENING:
@ -400,6 +404,8 @@ fhandler_fifo::listen_client_thread ()
} }
} }
out: out:
if (ret < 0)
debug_printf ("exiting lct with error, %E");
ResetEvent (read_ready); ResetEvent (read_ready);
return ret; return ret;
} }
@ -829,14 +835,15 @@ int
fifo_client_handler::close () fifo_client_handler::close ()
{ {
int res = 0; int res = 0;
HANDLE evt = InterlockedExchangePointer (&connect_evt, NULL);
if (evt)
CloseHandle (evt);
if (fh) if (fh)
{ {
res = fh->fhandler_base::close (); res = fh->fhandler_base::close ();
delete fh; delete fh;
} }
if (connect_evt)
CloseHandle (connect_evt);
return res; return res;
} }
@ -913,11 +920,7 @@ fhandler_fifo::dup (fhandler_base *child, int flags)
if (!DuplicateHandle (GetCurrentProcess (), fc_handler[i].fh->get_handle (), if (!DuplicateHandle (GetCurrentProcess (), fc_handler[i].fh->get_handle (),
GetCurrentProcess (), GetCurrentProcess (),
&fhf->fc_handler[i].fh->get_handle (), &fhf->fc_handler[i].fh->get_handle (),
0, true, DUPLICATE_SAME_ACCESS) 0, true, DUPLICATE_SAME_ACCESS))
|| !DuplicateHandle (GetCurrentProcess (), fc_handler[i].connect_evt,
GetCurrentProcess (),
&fhf->fc_handler[i].connect_evt,
0, true, DUPLICATE_SAME_ACCESS))
{ {
CloseHandle (fhf->read_ready); CloseHandle (fhf->read_ready);
CloseHandle (fhf->write_ready); CloseHandle (fhf->write_ready);