summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSalvatore Sanfilippo <antirez@gmail.com>2017-12-14 12:15:46 +0100
committerGitHub <noreply@github.com>2017-12-14 12:15:46 +0100
commit5182dcd6452ba3f03196124bcccfa8f80dd61fd1 (patch)
treea631b836ee49d19efd40a70665b7bd6ef35532c6
parentde276b6a439179b3cd0c94df82729cae8ab1cb9a (diff)
parent1b5f56d0428a8ea7cea4e6d501dbece7ff987578 (diff)
downloadredis-5182dcd6452ba3f03196124bcccfa8f80dd61fd1.tar.gz
Merge pull request #4498 from soloestoy/aof-safe-write
Aof safe write -- fix the short write
-rw-r--r--src/aof.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/aof.c b/src/aof.c
index 79962fd0a..3623ee885 100644
--- a/src/aof.c
+++ b/src/aof.c
@@ -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
@@ -342,7 +363,7 @@ void flushAppendOnlyFile(int force) {
/* We performed the write so reset the postponed flush sentinel to zero. */
server.aof_flush_postponed_start = 0;
- if (nwritten != (signed)sdslen(server.aof_buf)) {
+ if (nwritten != (ssize_t)sdslen(server.aof_buf)) {
static time_t last_write_error_log = 0;
int can_log = 0;