GeezerVison is showing github.com — Kqueue: sendfile EINTR doesn't advance offset — data duplication (#16… · netty/netty@b615afc
File tree
Expand file treeCollapse file tree
Open diff view settings
transport-native-kqueue/src/main/c
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: transport-native-kqueue/src/main/c/netty_kqueue_bsdsocket.c +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,12 +73,17 @@ static jlong netty_kqueue_bsdsocket_sendFile(JNIEnv* env, jclass clazz, jint soc |
73 | 73 | sbytes = 0; |
74 | 74 | res = sendfile(srcFd, socketFd, base_off + off, len, NULL, &sbytes, 0); |
75 | 75 | #endif |
| 76 | + // BSD/macOS sendfile passes the offset by value (unlike Linux which takes off_t*). |
| 77 | + // When interrupted (EINTR), sbytes reports how many bytes were sent before the signal. |
| 78 | + // Advance off so the next iteration resumes from where we left off, not from the start. |
| 79 | + off += sbytes; |
76 | 80 | len -= sbytes; |
77 | 81 | } while (res < 0 && ((err = errno) == EINTR)); |
78 | 82 | sbytes = lenBefore - len; |
79 | 83 | if (sbytes > 0) { |
80 | 84 | // update the transferred field in DefaultFileRegion |
81 | | - (*env)->SetLongField(env, fileRegion, transferredFieldId, off + sbytes); |
| 85 | + // off has already been advanced by sbytes inside the loop, so it equals the new total. |
| 86 | + (*env)->SetLongField(env, fileRegion, transferredFieldId, off); |
82 | 87 | return sbytes; |
83 | 88 | } |
84 | 89 | return res < 0 ? -err : 0; |
|