Skip to content

Commit 373a40a

Browse files
authored
Merge pull request #175 from 6WIND/io-eagain
io fixes on non-blocking FDs
2 parents 287043a + c0dbee8 commit 373a40a

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/io.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_time
7575
{
7676
size_t readd = 0;
7777
ssize_t r = -1;
78-
int fd;
78+
int fd, interrupted;
7979
struct timespec ts_cur, ts_inact_timeout;
8080

8181
assert(session);
@@ -92,6 +92,7 @@ nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_time
9292
nc_gettimespec_mono(&ts_inact_timeout);
9393
nc_addtimespec(&ts_inact_timeout, inact_timeout);
9494
do {
95+
interrupted = 0;
9596
switch (session->ti_type) {
9697
case NC_TI_NONE:
9798
return 0;
@@ -102,9 +103,13 @@ nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_time
102103
/* read via standard file descriptor */
103104
r = read(fd, buf + readd, count - readd);
104105
if (r < 0) {
105-
if ((errno == EAGAIN) || (errno == EINTR)) {
106+
if (errno == EAGAIN) {
106107
r = 0;
107108
break;
109+
} else if (errno == EINTR) {
110+
r = 0;
111+
interrupted = 1;
112+
break;
108113
} else {
109114
ERR("Session %u: reading from file descriptor (%d) failed (%s).",
110115
session->id, fd, strerror(errno));
@@ -190,7 +195,9 @@ nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_time
190195

191196
if (r == 0) {
192197
/* nothing read */
193-
usleep(NC_TIMEOUT_STEP);
198+
if (!interrupted) {
199+
usleep(NC_TIMEOUT_STEP);
200+
}
194201
nc_gettimespec_mono(&ts_cur);
195202
if ((nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) || (nc_difftimespec(&ts_cur, ts_act_timeout) < 1)) {
196203
if (nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) {
@@ -700,7 +707,7 @@ struct wclb_arg {
700707
static int
701708
nc_write(struct nc_session *session, const void *buf, size_t count)
702709
{
703-
int c, fd;
710+
int c, fd, interrupted;
704711
size_t written = 0;
705712
#ifdef NC_ENABLED_TLS
706713
unsigned long e;
@@ -721,13 +728,17 @@ nc_write(struct nc_session *session, const void *buf, size_t count)
721728
DBG("Session %u: sending message:\n%.*s\n", session->id, count, buf);
722729

723730
do {
731+
interrupted = 0;
724732
switch (session->ti_type) {
725733
case NC_TI_FD:
726734
case NC_TI_UNIX:
727735
fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock;
728736
c = write(fd, (char *)(buf + written), count - written);
729737
if (c < 0 && errno == EAGAIN) {
730738
c = 0;
739+
} else if (c < 0 && errno == EINTR) {
740+
c = 0;
741+
interrupted = 1;
731742
} else if (c < 0) {
732743
ERR("Session %u: socket error (%s).", session->id, strerror(errno));
733744
return -1;
@@ -787,7 +798,7 @@ nc_write(struct nc_session *session, const void *buf, size_t count)
787798
return -1;
788799
}
789800

790-
if (c == 0) {
801+
if (c == 0 && !interrupted) {
791802
/* we must wait */
792803
usleep(NC_TIMEOUT_STEP);
793804
}

0 commit comments

Comments
 (0)