Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve connection cleanup to avoid many error messages with ioc exit on some operating systems #200

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions asyn/drvAsynSerial/drvAsynIPServerPort.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static void connectionListener(void *drvPvt)
asynPrint(pasynUser, ASYN_TRACE_FLOW,
"drvAsynIPServerPort: %s started listening for connections on %s\n",
tty->portName, tty->serverInfo);
while (1) {
while (tty->fd != INVALID_SOCKET) {
if (tty->socketType == SOCK_DGRAM) {
if ((tty->UDPbufferPos == 0) && (tty->UDPbufferSize == 0)) {
tty->UDPbufferSize = recvfrom(tty->fd, tty->UDPbuffer, THEORETICAL_UDP_MAX_SIZE , 0, NULL, NULL);
Expand All @@ -324,6 +324,12 @@ static void connectionListener(void *drvPvt)

} else {
clientFd = epicsSocketAccept(tty->fd, (struct sockaddr *) &clientAddr, &clientLen);
if (tty->fd == INVALID_SOCKET) {
asynPrint(pasynUser, ASYN_TRACE_FLOW,
"drvAsynIPServerPort: terminating connection thread for %s\n",
tty->serverInfo);
break; /* we must be in ioc shutdown and ttyCleanup has been called */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or maybe the file descriptor limit was reached and accept() has returned with errno==EMFILE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add a separate test for this condition when we check clientFd at https://github.com/FreddieAkeroyd/asyn/blob/19db7d33e5576393f99feeb196f88b7d321e5a95/asyn/drvAsynSerial/drvAsynIPServerPort.c#L336 or would this situation also affect tty->fd ?

}
asynPrint(pasynUser, ASYN_TRACE_FLOW,
"drvAsynIPServerPort: new connection, socket=%d on %s\n",
clientFd, tty->serverInfo);
Expand Down Expand Up @@ -497,7 +503,13 @@ static void ttyCleanup(void *pPvt)
if (!tty) return;
if (tty->fd >= 0) {
asynPrint(tty->pasynUser, ASYN_TRACE_FLOW, "drvAsynIPServerPort:ttyCleanup %s: shutdown socket %d\n", tty->portName, tty->fd);
epicsSocketDestroy(tty->fd);
SOCKET fd_save = tty->fd;
tty->fd = INVALID_SOCKET;
/* shutdown then close the socket so accept thread will terminate */
shutdown(fd_save, SHUT_RDWR);
epicsThreadSleep(0.01); /* give time for shutdown to be noticed prior to close */
epicsSocketDestroy(fd_save);
epicsThreadSleep(0.1);
}
free(tty->portName);
free(tty->serverInfo);
Expand Down