Title: Race condition with Process.GetCurrentProcess().Handle in WindowsLoader.GetAllModules · Issue #1939 · pythonnet/pythonnet · GitHub
Open Graph Title: Race condition with Process.GetCurrentProcess().Handle in WindowsLoader.GetAllModules · Issue #1939 · pythonnet/pythonnet
X Title: Race condition with Process.GetCurrentProcess().Handle in WindowsLoader.GetAllModules · Issue #1939 · pythonnet/pythonnet
Description: Environment Pythonnet version: 3.0.0-rc4 Python version: 3.7.7 Operating System: Windows 10 21H2 .NET Runtime: 5.0.17 Details I think there might be a race condition in Python.Runtime.Platform.WindowsLoader.GetAllModules. I am hitting th...
Open Graph Description: Environment Pythonnet version: 3.0.0-rc4 Python version: 3.7.7 Operating System: Windows 10 21H2 .NET Runtime: 5.0.17 Details I think there might be a race condition in Python.Runtime.Platform.Wind...
X Description: Environment Pythonnet version: 3.0.0-rc4 Python version: 3.7.7 Operating System: Windows 10 21H2 .NET Runtime: 5.0.17 Details I think there might be a race condition in Python.Runtime.Platform.Wind...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/1939
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Race condition with Process.GetCurrentProcess().Handle in WindowsLoader.GetAllModules","articleBody":"### Environment\r\n\r\n- Pythonnet version: 3.0.0-rc4\r\n- Python version: 3.7.7\r\n- Operating System: Windows 10 21H2\r\n- .NET Runtime: 5.0.17\r\n\r\n### Details\r\n\r\n- I think there might be a race condition in `Python.Runtime.Platform.WindowsLoader.GetAllModules`. I am hitting this very sporadically when I initialize Python.NET from Python. The outer and inner exceptions are:\r\n```\r\nSystem.TypeInitializationException: The type initializer for 'Delegates' threw an exception.\r\n ---\u003e System.ComponentModel.Win32Exception (6): The handle is invalid.\r\n at Python.Runtime.Platform.WindowsLoader.GetAllModules() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Native/LibraryLoader.cs:line 153\r\n at Python.Runtime.Platform.WindowsLoader.GetFunction(IntPtr hModule, String procedureName) in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Native/LibraryLoader.cs:line 133\r\n at Python.Runtime.Runtime.Delegates.GetFunctionByName(String functionName, IntPtr libraryHandle) in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Runtime.Delegates.cs:line 296\r\n at Python.Runtime.Runtime.Delegates..cctor() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Runtime.Delegates.cs:line 110\r\n --- End of inner exception stack trace ---\r\n at Python.Runtime.Runtime.Delegates.get_PyGILState_Ensure() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Runtime.Delegates.cs:line 319\r\n at Python.Runtime.Runtime.PyGILState_Ensure() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Runtime.cs:line 747\r\n at Python.Runtime.PythonEngine.AcquireLock() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/PythonEngine.cs:line 472\r\n at Python.Runtime.Py.GILState..ctor() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Py.cs:line 27\r\n at Python.Runtime.Py.GIL() in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Py.cs:line 13\r\n at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size) in /tmp/build-via-sdist-sstjjspf/pythonnet-3.0.0rc4/src/runtime/Loader.cs:line 26\r\n```\r\n\r\nLine numbers don't seem to line up exactly for me, but when I'm able to catch it in the debugger, it's the `throw new Win32Exception()` lines that trigger it.\r\n\r\nI think what might be happening in my case is that the handle `self` here: https://github.com/pythonnet/pythonnet/blob/v3.0.0-rc4/src/runtime/Native/LibraryLoader.cs#L140\r\nis being closed before one of the calls to `EnumProcessModules`.\r\n\r\nThe docs for `Process.GetCurrentProcess()` say that each invocation returns a new `Process` component; each instance opens its own handle to the current process, and that handle gets closed when `Process.Close` or `Process.Dispose` is called. Since a reference isn't being retained to the object returned by `Process.GetCurrentProcess()`, the garbage collector is able to collect it and its handle would be closed, which means that `self` is no longer valid.\r\n\r\nI think one solution would be something like...\r\n```\r\nusing (Process p = Process.GetCurrentProcess())\r\n{\r\n EnumProcessModules(p.Handle, ...\r\n}\r\n```\r\n\r\nin order to keep the process alive while its handle is still being accessed. Thanks!","author":{"url":"https://github.com/sdao","@type":"Person","name":"sdao"},"datePublished":"2022-09-15T20:54:19.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1939/pythonnet/issues/1939"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:34f21e7a-c5d3-0c9c-bf4e-6f66a481450a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B18C:A87A4:FE18C9:162CE21:6970C5C7 |
| html-safe-nonce | 6d9dbf1e09b23f4569d53b739b3fd3791a57d781960c11e4fb1b91933426b0d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMThDOkE4N0E0OkZFMThDOToxNjJDRTIxOjY5NzBDNUM3IiwidmlzaXRvcl9pZCI6IjkxNDcwNDIyMjM0MzY2NDU4MzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 841291b1935474a21f8d5f9a4f5be9021b7398e2fdf935cf61895f4fd0bc9bd2 |
| hovercard-subject-tag | issue:1375078055 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/1939/issue_layout |
| twitter:image | https://opengraph.githubassets.com/095b2d38239f47581b52f195f8733bdcd8cac99bb450ea0a958f204ed126400b/pythonnet/pythonnet/issues/1939 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/095b2d38239f47581b52f195f8733bdcd8cac99bb450ea0a958f204ed126400b/pythonnet/pythonnet/issues/1939 |
| og:image:alt | Environment Pythonnet version: 3.0.0-rc4 Python version: 3.7.7 Operating System: Windows 10 21H2 .NET Runtime: 5.0.17 Details I think there might be a race condition in Python.Runtime.Platform.Wind... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sdao |
| hostname | github.com |
| expected-hostname | github.com |
| None | bb43a7bc61aba1b91c3c5cf8e7d00342e1e77a0cfe55a141222dbd7f9782d26f |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 34817b01ad7cdf8b2beb35ea7b0e2a7609004eff |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width