Skip to content

Commit 5cc94e5

Browse files
authored
Improve posix mmap retry logic (#3714)
- Only retry on EAGAIN, ENOMEM or EINTR. - On EINTR, don't count it against the retry budget, just keep retrying. EINTR can happen in bursts. - Log the errno on failure, and don't conditionalize that logging on BH_ENABLE_TRACE_MMAP. In other parts of the code, error logging is not conditional on that define, while turning on that tracing define makes things overly verbose.
1 parent 0b62cc8 commit 5cc94e5

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

core/shared/platform/common/posix/posix_memmap.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,25 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
138138

139139
/* memory hasn't been mapped or was mapped failed previously */
140140
if (addr == MAP_FAILED) {
141-
/* try 5 times */
142-
for (i = 0; i < 5; i++) {
141+
/* try 5 times on EAGAIN or ENOMEM, and keep retrying on EINTR */
142+
i = 0;
143+
while (i < 5) {
143144
addr = mmap(hint, request_size, map_prot, map_flags, file, 0);
144145
if (addr != MAP_FAILED)
145146
break;
147+
if (errno == EINTR)
148+
continue;
149+
if (errno != EAGAIN && errno != ENOMEM) {
150+
break;
151+
}
152+
i++;
146153
}
147154
}
148155

149156
if (addr == MAP_FAILED) {
150-
#if BH_ENABLE_TRACE_MMAP != 0
151-
os_printf("mmap failed\n");
152-
#endif
157+
os_printf("mmap failed with errno: %d, hint: %p, size: %" PRIu64
158+
", prot: %d, flags: %d",
159+
errno, hint, request_size, map_prot, map_flags);
153160
return NULL;
154161
}
155162

0 commit comments

Comments
 (0)