Skip to content

Instantly share code, notes, and snippets.

@raphael-proust
Created September 18, 2020 07:59
Show Gist options
  • Save raphael-proust/78ae49b40757b1475d9e4e18c51cfdab to your computer and use it in GitHub Desktop.
Save raphael-proust/78ae49b40757b1475d9e4e18c51cfdab to your computer and use it in GitHub Desktop.
WIP for ERANGE handling
diff --git a/src/unix/lwt_unix.cppo.ml b/src/unix/lwt_unix.cppo.ml
index 89bb84228..69ef1a45d 100644
--- a/src/unix/lwt_unix.cppo.ml
+++ b/src/unix/lwt_unix.cppo.ml
@@ -1543,29 +1543,48 @@ let getpwnam name =
else
run_job (getpwnam_job name)
-external getgrnam_job : string -> Unix.group_entry job = "lwt_unix_getgrnam_job"
+let retry_after_erange name trying =
+ let rec loop factor =
+ try
+ trying factor
+ with
+ | Unix.Unix_error(Unix.ERANGE, namename, _) -> begin
+ assert (name = namename);
+ loop (factor * 2)
+ end
+ in
+ loop 1
+
+
+external getgrnam_job : (string * int) -> Unix.group_entry job = "lwt_unix_getgrnam_job"
let getgrnam name =
if Sys.win32 || Lwt_config.android then
Lwt.return (Unix.getgrnam name)
else
- run_job (getgrnam_job name)
+ retry_after_erange "getgrnam" (fun factor ->
+ run_job (getgrnam_job (name, factor))
+ )
-external getpwuid_job : int -> Unix.passwd_entry job = "lwt_unix_getpwuid_job"
+external getpwuid_job : (int * int) -> Unix.passwd_entry job = "lwt_unix_getpwuid_job"
let getpwuid uid =
if Sys.win32 || Lwt_config.android then
Lwt.return (Unix.getpwuid uid)
else
- run_job (getpwuid_job uid)
+ retry_after_erange "getpwuid" (fun factor ->
+ run_job (getpwuid_job (uid, factor))
+ )
-external getgrgid_job : int -> Unix.group_entry job = "lwt_unix_getgrgid_job"
+external getgrgid_job : (int * int) -> Unix.group_entry job = "lwt_unix_getgrgid_job"
let getgrgid gid =
if Sys.win32 || Lwt_config.android then
Lwt.return (Unix.getgrgid gid)
else
- run_job (getgrgid_job gid)
+ retry_after_erange "getgrnam" (fun factor ->
+ run_job (getgrgid_job (gid, factor))
+ )
(* +-----------------------------------------------------------------+
| Sockets |
diff --git a/src/unix/unix_c/unix_get_pw_gr_nam_id_job.c b/src/unix/unix_c/unix_get_pw_gr_nam_id_job.c
index e256a2963..d4cd0364f 100644
--- a/src/unix/unix_c/unix_get_pw_gr_nam_id_job.c
+++ b/src/unix/unix_c/unix_get_pw_gr_nam_id_job.c
@@ -16,7 +16,6 @@
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
-#include <errno.h>
#include "lwt_unix.h"
@@ -78,21 +77,14 @@ static value alloc_group_entry(struct group *entry)
ARG_DECL; \
}; \
\
- static void worker_##FUNC(struct job_##FUNC *job) \
+ static void worker_##FUNC(struct job_##FUNC *job, int factor) \
{ \
size_t buffer_size = sysconf(_SC_##CONF##_R_SIZE_MAX); \
if (buffer_size == (size_t)-1) buffer_size = 16384; \
+ buffer_size = buffer_size * factor; \
job->buffer = (char *)lwt_unix_malloc(buffer_size); \
job->result = FUNC##_r(job->ARG, &job->entry, job->buffer, \
buffer_size, &job->ptr); \
- if (job->result == -1) { \
- if (errno == ERANGE) { \
- buffer_size = buffer_size *2; \
- job->buffer = (char *)lwt_unix_malloc(buffer_size); \
- job->result = FUNC##_r(job->ARG, &job->entry, job->buffer, \
- buffer_size, &job->ptr); \
- } \
- } \
} \
\
static value result_##FUNC(struct job_##FUNC *job) \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment