|
| 1 | +From ee6e4a3eb09aebc2ed9fe7959c4f997b5e75664f Mon Sep 17 00:00:00 2001 |
| 2 | +From: "Erik M. Bray" < [email protected]> |
| 3 | +Date: Tue, 19 Mar 2019 16:34:15 +0100 |
| 4 | +Subject: [PATCH] Trac #27509: Add patch from upstream pull request to fix |
| 5 | + memory leak on Cygwin |
| 6 | + |
| 7 | +--- |
| 8 | + build/pkgs/openblas/patches/2064.patch | 137 +++++++++++++++++++++++++++++++++ |
| 9 | + 1 file changed, 137 insertions(+) |
| 10 | + create mode 100644 build/pkgs/openblas/patches/2064.patch |
| 11 | + |
| 12 | +diff --git a/build/pkgs/openblas/patches/2064.patch b/build/pkgs/openblas/patches/2064.patch |
| 13 | +new file mode 100644 |
| 14 | +index 0000000000..399277a00e |
| 15 | +--- /dev/null |
| 16 | ++++ b/build/pkgs/openblas/patches/2064.patch |
| 17 | +@@ -0,0 +1,137 @@ |
| 18 | ++Fixes memory leak in Cygwin that occurs when repeatedly shutting down |
| 19 | ++and re-intializing the the thread pool, e.g. when forking the process. |
| 20 | ++See https://trac.sagemath.org/ticket/27509 |
| 21 | ++ |
| 22 | ++From 4ad694eda1ff79040778648d44cda5b8f774c38d Mon Sep 17 00:00:00 2001 |
| 23 | ++From: "Erik M. Bray" <[email protected]> |
| 24 | ++Date: Mon, 18 Mar 2019 20:32:48 +0100 |
| 25 | ++Subject: [PATCH 1/2] Fix for #2063: The DllMain used in Cygwin did not run the |
| 26 | ++ thread memory pool cleanup upon THREAD_DETACH which is needed when compiled |
| 27 | ++ with USE_TLS=1. |
| 28 | ++ |
| 29 | ++--- |
| 30 | ++ driver/others/memory.c | 11 +++++++++-- |
| 31 | ++ exports/dllinit.c | 24 +++++++++++++++++------- |
| 32 | ++ 2 files changed, 26 insertions(+), 9 deletions(-) |
| 33 | ++ |
| 34 | ++diff --git a/driver/others/memory.c b/driver/others/memory.c |
| 35 | ++index ed407a858..ac8545f35 100644 |
| 36 | ++--- a/driver/others/memory.c |
| 37 | +++++ b/driver/others/memory.c |
| 38 | ++@@ -1313,6 +1313,13 @@ void blas_memory_free_nolock(void * map_address) { |
| 39 | ++ free(map_address); |
| 40 | ++ } |
| 41 | ++ |
| 42 | +++#ifdef SMP |
| 43 | +++void blas_thread_memory_cleanup(void) { |
| 44 | +++ blas_memory_cleanup((void*)get_memory_table()); |
| 45 | +++} |
| 46 | +++#endif |
| 47 | +++ |
| 48 | +++ |
| 49 | ++ void blas_shutdown(void){ |
| 50 | ++ #ifdef SMP |
| 51 | ++ BLASFUNC(blas_thread_shutdown)(); |
| 52 | ++@@ -1322,7 +1329,7 @@ void blas_shutdown(void){ |
| 53 | ++ /* Only cleanupIf we were built for threading and TLS was initialized */ |
| 54 | ++ if (local_storage_key) |
| 55 | ++ #endif |
| 56 | ++- blas_memory_cleanup((void*)get_memory_table()); |
| 57 | +++ blas_thread_memory_cleanup(); |
| 58 | ++ |
| 59 | ++ #ifdef SEEK_ADDRESS |
| 60 | ++ base_address = 0UL; |
| 61 | ++@@ -1552,7 +1559,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReser |
| 62 | ++ break; |
| 63 | ++ case DLL_THREAD_DETACH: |
| 64 | ++ #if defined(SMP) |
| 65 | ++- blas_memory_cleanup((void*)get_memory_table()); |
| 66 | +++ blas_thread_memory_cleanup(); |
| 67 | ++ #endif |
| 68 | ++ break; |
| 69 | ++ case DLL_PROCESS_DETACH: |
| 70 | ++diff --git a/exports/dllinit.c b/exports/dllinit.c |
| 71 | ++index 02ff092e9..0e1bb34e3 100644 |
| 72 | ++--- a/exports/dllinit.c |
| 73 | +++++ b/exports/dllinit.c |
| 74 | ++@@ -40,15 +40,25 @@ |
| 75 | ++ |
| 76 | ++ void gotoblas_init(void); |
| 77 | ++ void gotoblas_quit(void); |
| 78 | +++#if defined(SMP) && defined(USE_TLS) |
| 79 | +++void blas_thread_memory_cleanup(void); |
| 80 | +++#endif |
| 81 | ++ |
| 82 | ++ BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD reason, LPVOID reserved) { |
| 83 | ++- |
| 84 | ++- if (reason == DLL_PROCESS_ATTACH) { |
| 85 | ++- gotoblas_init(); |
| 86 | ++- } |
| 87 | ++- |
| 88 | ++- if (reason == DLL_PROCESS_DETACH) { |
| 89 | ++- gotoblas_quit(); |
| 90 | +++ switch(reason) { |
| 91 | +++ case DLL_PROCESS_ATTACH: |
| 92 | +++ gotoblas_init(); |
| 93 | +++ break; |
| 94 | +++ case DLL_PROCESS_DETACH: |
| 95 | +++ gotoblas_quit(); |
| 96 | +++ break; |
| 97 | +++ case DLL_THREAD_ATTACH: |
| 98 | +++ break; |
| 99 | +++ case DLL_THREAD_DETACH: |
| 100 | +++#if defined(SMP) && defined(USE_TLS) |
| 101 | +++ blas_thread_memory_cleanup(void); |
| 102 | +++#endif |
| 103 | +++ break; |
| 104 | ++ } |
| 105 | ++ |
| 106 | ++ return TRUE; |
| 107 | ++ |
| 108 | ++From 8ba9e2a61a1cf34e9b2efc5af61f5ebaaf6ab902 Mon Sep 17 00:00:00 2001 |
| 109 | ++From: "Erik M. Bray" <[email protected]> |
| 110 | ++Date: Tue, 19 Mar 2019 10:22:02 +0100 |
| 111 | ++Subject: [PATCH 2/2] Also call CloseHandle on each thread, as well as on the |
| 112 | ++ event so as to not leak thread handles. |
| 113 | ++ |
| 114 | ++--- |
| 115 | ++ driver/others/blas_server_win32.c | 5 +++++ |
| 116 | ++ exports/dllinit.c | 2 +- |
| 117 | ++ 2 files changed, 6 insertions(+), 1 deletion(-) |
| 118 | ++ |
| 119 | ++diff --git a/driver/others/blas_server_win32.c b/driver/others/blas_server_win32.c |
| 120 | ++index bae344c59..0b38ee365 100644 |
| 121 | ++--- a/driver/others/blas_server_win32.c |
| 122 | +++++ b/driver/others/blas_server_win32.c |
| 123 | ++@@ -461,13 +461,18 @@ int BLASFUNC(blas_thread_shutdown)(void){ |
| 124 | ++ SetEvent(pool.killed); |
| 125 | ++ |
| 126 | ++ for(i = 0; i < blas_num_threads - 1; i++){ |
| 127 | +++ // Could also just use WaitForMultipleObjects |
| 128 | ++ WaitForSingleObject(blas_threads[i], 5); //INFINITE); |
| 129 | ++ #ifndef OS_WINDOWSSTORE |
| 130 | ++ // TerminateThread is only available with WINAPI_DESKTOP and WINAPI_SYSTEM not WINAPI_APP in UWP |
| 131 | ++ TerminateThread(blas_threads[i],0); |
| 132 | ++ #endif |
| 133 | +++ CloseHandle(blas_threads[i]); |
| 134 | ++ } |
| 135 | ++ |
| 136 | +++ CloseHandle(pool.filled); |
| 137 | +++ CloseHandle(pool.killed); |
| 138 | +++ |
| 139 | ++ blas_server_avail = 0; |
| 140 | ++ } |
| 141 | ++ |
| 142 | ++diff --git a/exports/dllinit.c b/exports/dllinit.c |
| 143 | ++index 0e1bb34e3..4a05c0e14 100644 |
| 144 | ++--- a/exports/dllinit.c |
| 145 | +++++ b/exports/dllinit.c |
| 146 | ++@@ -56,7 +56,7 @@ BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD reason, LPVOID reserved) { |
| 147 | ++ break; |
| 148 | ++ case DLL_THREAD_DETACH: |
| 149 | ++ #if defined(SMP) && defined(USE_TLS) |
| 150 | ++- blas_thread_memory_cleanup(void); |
| 151 | +++ blas_thread_memory_cleanup(); |
| 152 | ++ #endif |
| 153 | ++ break; |
| 154 | ++ } |
| 155 | +-- |
| 156 | +2.15.1 |
| 157 | + |
0 commit comments