diff options
author | zhaozhao.zz <zhaozhao.zz@alibaba-inc.com> | 2017-11-30 10:22:12 +0800 |
---|---|---|
committer | zhaozhao.zz <zhaozhao.zz@alibaba-inc.com> | 2017-11-30 10:22:12 +0800 |
commit | 2d73cf236725e0f37394eed20640670a4888d426 (patch) | |
tree | ebb8bd667c68a19447873253382260b1934c5974 | |
parent | 75fa7879e6b4408247db6b7eba3def64c00d4f9a (diff) | |
download | redis-2d73cf236725e0f37394eed20640670a4888d426.tar.gz |
aof: fix the short write
-rw-r--r-- | src/aof.c | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -266,6 +266,27 @@ int startAppendOnly(void) { return C_OK; } +ssize_t safe_write(int fd, const char *buf, size_t len) { + ssize_t nwritten = 0, totwritten = 0; + + while(len) { + nwritten = write(fd, buf, len); + + if (nwritten < 0) { + if (errno == EINTR) { + continue; + } + return totwritten ? totwritten : -1; + } + + len -= nwritten; + buf += nwritten; + totwritten += nwritten; + } + + return totwritten; +} + /* Write the append only file buffer on disk. * * Since we are required to write the AOF before replying to the client, @@ -323,7 +344,7 @@ void flushAppendOnlyFile(int force) { * or alike */ latencyStartMonitor(latency); - nwritten = write(server.aof_fd,server.aof_buf,sdslen(server.aof_buf)); + nwritten = safe_write(server.aof_fd,server.aof_buf,sdslen(server.aof_buf)); latencyEndMonitor(latency); /* We want to capture different events for delayed writes: * when the delay happens with a pending fsync, or with a saving child |