-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process.environ() on Windows may raise WindowsError #875
Comments
@fbenkstein do you mind taking a look at this or do you have any suggestion? psutil/psutil/arch/windows/process_info.c Line 663 in 7c36a63
|
@giampaolo how often does this occur? I assume it isn't reproducible reliably. Unfortunately I don't have a Windows dev environment at the moment so I can't debug this myself. Maybe it's possible that we're trying to read the process memory when it isn't fully created yet or otherwise the test machine was so busy that the test process exited while we were trying to read its memory. |
I can reproduce it if I run that test in a loop. After a while (say 20-30 times) I bump into the exception. I suppose in that case we could realloc the buffer with a bigger size and retry. I will try that. |
I don't think it's the buffer size. If it's too small you'll simply read less. IIRC increasing the buffer will probably increase the likelihood of the error since you might try to read memory beyond what is actually valid in the process. If you want to handle the error, it's probably better to check if the process is still alive, though I think this should be left up to the caller of environ(). |
In that case (unit test) the process is alive for sure. |
It turns out this also occurs for |
Workaround for giampaolo#875 (comment)
Workaround for giampaolo#875 (comment)
Workaround for giampaolo#875 (comment)
Workaround for giampaolo#875 (comment)
Workaround for giampaolo#875 (comment)
Workaround for giampaolo#875 (comment)
@giampaolo would you consider a workaround for |
I forgot about this one and I haven't seen it in a while meaning that (maybe) it got fixed but I forgot to update this issue. Can you reproduce it with latest version? |
Yes, it's reproducible given enough tries. |
It can be reproduced with this: import os
import psutil
import subprocess
def find_pid_by_name(name):
"Return a list of processes matching 'name'."
ls = []
for p in list(psutil.process_iter(attrs=["name", "exe", "cmdline"])):
if name == p.info['name'] or \
p.info['exe'] and os.path.basename(p.info['exe']) == name or \
p.info['cmdline'] and p.info['cmdline'][0] == name:
ls.append(p)
if len(ls) == 1:
return ls[0].pid
return -1
for i in range(10000):
info = subprocess.STARTUPINFO()
info.dwFlags = 1
info.wShowWindow = 0
process = subprocess.Popen(
"notepad.exe",
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NEW_CONSOLE,
startupinfo=info)
while True:
before = find_pid_by_name("notepad.exe")
if before != -1:
os.kill(before, 9)
else:
break
after = find_pid_by_name("notepad.exe")
print(before, after)
if before != after and after == -1:
break |
Workaround for giampaolo#875 (comment)
Just saw this error in v5.6.3.
|
@giampaolo Thanks for your work on this issue. I wonder though if it would be more sensible to retry once or twice before returning If |
I agree with you. Thanks for the insight. |
<function Process.environ at 0x0000026552DA0E18> retrieed 33 times, converted to AccessDenied as it's still returning OSError(22,'Only part of a ReadProcessMemory or WriteProcessMemory request was completed.') I also got this error on Windows in psutil == 5.9.4, which looks similar to this problem |
https://ci.appveyor.com/project/giampaolo/psutil/build/806/job/fpxxbai3sg291i75
The text was updated successfully, but these errors were encountered: