Skip to content

Commit

Permalink
[#115] Only close known sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Nov 2, 2020
1 parent 966e86e commit 5e01f52
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ pgagroal_socket_nonblocking(int fd, bool value);
bool
pgagroal_socket_is_nonblocking(int fd);

/**
* Does the socket have an error associated
* @param fd The descriptor
* @return 0 upon success, otherwise 1
*/
int
pgagroal_socket_has_error(int fd);

#ifdef __cplusplus
}
#endif
Expand Down
27 changes: 27 additions & 0 deletions src/libpgagroal/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,33 @@ pgagroal_socket_is_nonblocking(int fd)
return flags & O_NONBLOCK;
}

int
pgagroal_socket_has_error(int fd)
{
int error = 0;
socklen_t length = sizeof(int);

if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &length) == -1)
{
ZF_LOGV("error getting socket error code: %s (%d)", strerror(errno), fd);
errno = 0;
goto error;
}

if (error != 0)
{
ZF_LOGV("socket error: %s (%d)", strerror(error), fd);
errno = 0;
goto error;
}

return 0;

error:

return 1;
}

int
pgagroal_tcp_nodelay(int fd, void* shmem)
{
Expand Down
5 changes: 4 additions & 1 deletion src/libpgagroal/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ pgagroal_kill_connection(void* shmem, int slot)
if (fd != -1)
{
pgagroal_management_kill_connection(shmem, slot, fd);
pgagroal_disconnect(fd);
if (!pgagroal_socket_has_error(fd))
{
pgagroal_disconnect(fd);
}
}
else
{
Expand Down

0 comments on commit 5e01f52

Please sign in to comment.