Skip to content

Commit c53669f

Browse files
authored
avoid loading duplicate libraries (#42058)
We will not use the duplicate, so best to try to avoid loading it.
1 parent c5af061 commit c53669f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cli/loader_lib.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@ void jl_loader_print_stderr3(const char * msg1, const char * msg2, const char *
3131

3232
/* Wrapper around dlopen(), with extra relative pathing thrown in*/
3333
static void * load_library(const char * rel_path, const char * src_dir) {
34+
void * handle = NULL;
35+
36+
// See if a handle is already open to the basename
37+
const char *basename = rel_path + strlen(rel_path);
38+
while (basename-- > rel_path)
39+
if (*basename == PATHSEPSTRING[0] || *basename == '/')
40+
break;
41+
basename++;
42+
#if defined(_OS_WINDOWS_)
43+
if ((handle = GetModuleHandleW(basename)))
44+
return handle;
45+
#else
46+
if ((handle = dlopen(basename, RTLD_NOLOAD | RTLD_NOW | RTLD_GLOBAL)))
47+
return handle;
48+
#endif
49+
3450
char path[2*PATH_MAX + 1] = {0};
3551
strncat(path, src_dir, sizeof(path) - 1);
3652
strncat(path, PATHSEPSTRING, sizeof(path) - 1);
3753
strncat(path, rel_path, sizeof(path) - 1);
3854

39-
void * handle = NULL;
4055
#if defined(_OS_WINDOWS_)
4156
wchar_t wpath[2*PATH_MAX + 1] = {0};
4257
if (!utf8_to_wchar(path, wpath, 2*PATH_MAX)) {

src/jl_exported_funcs.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,4 @@
551551
XX(jl_vprintf) \
552552
XX(jl_wakeup_thread) \
553553
XX(jl_yield) \
554+

0 commit comments

Comments
 (0)