René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:34f21e7a-c5d3-0c9c-bf4e-6f66a481450a
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB18C:A87A4:FE18C9:162CE21:6970C5C7
html-safe-nonce6d9dbf1e09b23f4569d53b739b3fd3791a57d781960c11e4fb1b91933426b0d2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMThDOkE4N0E0OkZFMThDOToxNjJDRTIxOjY5NzBDNUM3IiwidmlzaXRvcl9pZCI6IjkxNDcwNDIyMjM0MzY2NDU4MzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac841291b1935474a21f8d5f9a4f5be9021b7398e2fdf935cf61895f4fd0bc9bd2
hovercard-subject-tagissue:1375078055
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/1939/issue_layout
twitter:imagehttps://opengraph.githubassets.com/095b2d38239f47581b52f195f8733bdcd8cac99bb450ea0a958f204ed126400b/pythonnet/pythonnet/issues/1939
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/095b2d38239f47581b52f195f8733bdcd8cac99bb450ea0a958f204ed126400b/pythonnet/pythonnet/issues/1939
og:image:altEnvironment 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesdao
hostnamegithub.com
expected-hostnamegithub.com
Nonebb43a7bc61aba1b91c3c5cf8e7d00342e1e77a0cfe55a141222dbd7f9782d26f
turbo-cache-controlno-preview
go-importgithub.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git
octolytics-dimension-user_id6050430
octolytics-dimension-user_loginpythonnet
octolytics-dimension-repository_id14748123
octolytics-dimension-repository_nwopythonnet/pythonnet
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id14748123
octolytics-dimension-repository_network_root_nwopythonnet/pythonnet
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release34817b01ad7cdf8b2beb35ea7b0e2a7609004eff
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/pythonnet/pythonnet/issues/1939#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fissues%2F1939
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fissues%2F1939
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=pythonnet%2Fpythonnet
Reloadhttps://github.com/pythonnet/pythonnet/issues/1939
Reloadhttps://github.com/pythonnet/pythonnet/issues/1939
Reloadhttps://github.com/pythonnet/pythonnet/issues/1939
pythonnet https://github.com/pythonnet
pythonnethttps://github.com/pythonnet/pythonnet
Notifications https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Fork 770 https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Star 5.4k https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Code https://github.com/pythonnet/pythonnet
Issues 155 https://github.com/pythonnet/pythonnet/issues
Pull requests 18 https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects 0 https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/pythonnet/pythonnet/security
Please reload this pagehttps://github.com/pythonnet/pythonnet/issues/1939
Insights https://github.com/pythonnet/pythonnet/pulse
Code https://github.com/pythonnet/pythonnet
Issues https://github.com/pythonnet/pythonnet/issues
Pull requests https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security https://github.com/pythonnet/pythonnet/security
Insights https://github.com/pythonnet/pythonnet/pulse
New issuehttps://github.com/login?return_to=https://github.com/pythonnet/pythonnet/issues/1939
New issuehttps://github.com/login?return_to=https://github.com/pythonnet/pythonnet/issues/1939
Race condition with Process.GetCurrentProcess().Handle in WindowsLoader.GetAllModuleshttps://github.com/pythonnet/pythonnet/issues/1939#top
https://github.com/sdao
https://github.com/sdao
sdaohttps://github.com/sdao
on Sep 15, 2022https://github.com/pythonnet/pythonnet/issues/1939#issue-1375078055
https://github.com/pythonnet/pythonnet/blob/v3.0.0-rc4/src/runtime/Native/LibraryLoader.cs#L140https://github.com/pythonnet/pythonnet/blob/v3.0.0-rc4/src/runtime/Native/LibraryLoader.cs#L140
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.