py-spy icon indicating copy to clipboard operation
py-spy copied to clipboard

Error: 仅完成部分的 ReadProcessMemory 或 WriteProcessMemory 请求。 (os error 299)

Open lihailong00 opened this issue 1 year ago • 9 comments

My Python version is 3.12.7 I use Windows 11 I run a python program in pycharm:

import os
import time


def main():
    print(os.getpid())
    print(1)
    time.sleep(100)
    print(2)
    time.sleep(200)
    print(3)


if __name__ == '__main__':
    main()

then print the process id: 58396

then I input "py-spy top --pid 58396" then Error occured:

Error: 仅完成部分的 ReadProcessMemory 或 WriteProcessMemory 请求。 (os error 299)
Reason: 仅完成部分的 ReadProcessMemory 或 WriteProcessMemory 请求。 (os error 299)

image

What's the promblems.

Thanks for your reply~

lihailong00 avatar Nov 20 '24 04:11 lihailong00

I am getting the same error:

Error: Only part of a ReadProcessMemory or WriteProcessMemory request was completed. (os error 299)
Reason: Only part of a ReadProcessMemory or WriteProcessMemory request was completed. (os error 299)

python 3.12 Windows 10 py-spy 0.4.0

tim-stephenson avatar Nov 21 '24 19:11 tim-stephenson

Here's my code, function find_urls_from_text is stuck in an endless loop. I use os.getpid() to get the current process number. Then I execute py-spy record -o profile.svg --pid in my terminal. then I am getting the error:

Error: Only part of a ReadProcessMemory or WriteProcessMemory request was completed. (os error 299)
Reason: Only part of a ReadProcessMemory or WriteProcessMemory request was completed. (os error 299)
import os
import re
from typing import List


def find_urls_from_text(text: str) -> List[str]:
    """
    get websites from urls
    :param text:
    :return:
    """
    url_pattern = (
        r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))'
    )

    # 查找所有匹配项
    urls = re.findall(url_pattern, text, re.IGNORECASE)

    # 提取完整的网址
    cleaned_urls = [''.join(match[0].split()) for match in urls]

    return cleaned_urls


print(os.getpid())
s = '中国政府采购网(http://www.ccgp.gov.cn)、广西壮族自治区政府采购网(http://zfcg.gxzf.gov.cn)、广西壮族自治区招标投标公共服务平台(http://zbtb.gxi.gov.cn:9000/)、全国公共资源交易平台(广西·百色)+(http://ggzy.jg+swj.gxzf.gov.cn/bsggzy/)、广西百色市住房和城乡建设局(http://zjj.baise.gov.cn)上发布'
res = find_urls_from_text(s)
print(res)

lihailong00 avatar Nov 26 '24 01:11 lihailong00

Same here. It's because of Python version. Py-spy doesn't support python 3.12 or 3.13 for sure yet. I can confirm that it works for Python 3.5. Also you can see Python 3.6 example in README.md file. But I don't know what is the maximum python version supported.

This error may be Windows only.

v01d-gh avatar Nov 27 '24 19:11 v01d-gh

I agree the reason for the issue is python 3.12. I have been able to use py-spy fine on Windows with python 3.10. However, py-spy seems to be currently claiming to have python 3.12 and 3.13 support (See 0.4.0 release notes https://github.com/benfred/py-spy/releases/tag/v0.4.0). Such support many be Linux / MacOS only. If that is the case it should be clearly stated to users.

tim-stephenson avatar Nov 27 '24 19:11 tim-stephenson

@benfred hey, I had a debugging session today. Something is off with the GIL retrieval routines. I suspect it is something related to atomic pointers, or rather the way we read them. addr always ends up being 0x100000000

diff --git a/src/stack_trace.rs b/src/stack_trace.rs
index cbfc51d..e271968 100644
--- a/src/stack_trace.rs
+++ b/src/stack_trace.rs
@@ -291,7 +291,7 @@ pub fn get_gil_threadid<I: InterpreterState, P: ProcessMemory>(
         let addr: usize = process.copy_struct(threadstate_address)?;
 
         // if the addr is 0, no thread is currently holding the GIL
-        if addr != 0 {
+        if addr != 0 && addr != 0x100000000 {
             let threadstate: I::ThreadState = process.copy_struct(addr)?;
             return Ok(threadstate.thread_id());
         }

akhramov avatar Dec 04 '24 00:12 akhramov

I have the same issue for Python 3.10, and the change akhramov suggested doesn't change anything for me.

filmor avatar Jan 27 '25 17:01 filmor

With Python 3.13 I was getting the same error on Windows.

Downgraded to 3.11 and it worked.

skrobchik avatar Feb 20 '25 14:02 skrobchik

py-spy currently claims to have Python 3.12 and 3.13 support, while I still face the same problem under py 3.12.8

perctrix avatar Apr 17 '25 01:04 perctrix

keep watching

zengruizhao avatar May 30 '25 02:05 zengruizhao

This is a P1 bug. Please correct the readme to state only up to Python 3.11. It does not support beyond that based on this thread nor my own experience.

O-J1 avatar Jun 18 '25 07:06 O-J1

This shoudl be fixed by https://github.com/benfred/py-spy/pull/774

benfred avatar Jul 12 '25 18:07 benfred

You can install the unreleased changes without having to compile yourself this way:

  1. go to https://github.com/benfred/py-spy/actions/workflows/build.yml
  2. click on the first run which lists master
  3. at the left side, click on build (windows-latest)
  4. find upload windows wheels (15th place) and expand it
  5. the last output should be Artifact download URL: - click on this

The downloaded zip file contains a wheel which you can pip install ./py_spy[...].whl

I didn't paste the URL here as these files might be temporary.

Maybe these files could be made more accessible, for example through a github pre-release?

laundmo avatar Jul 16 '25 10:07 laundmo

This shoudl be fixed by #774

The issue is resolved for me in the latest build. If confirmed by others, wouldn't it be worth a release?

My configuration:

joseph-sch avatar Jul 23 '25 13:07 joseph-sch

fix is in the 0.4.1 release https://github.com/benfred/py-spy/releases/tag/v0.4.1 -

benfred avatar Jul 31 '25 19:07 benfred