You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by "xlq20080808 (via GitHub)" <gi...@apache.org> on 2023/11/28 13:39:04 UTC
[I] [Bug] Python agent’s @runnable does not behave as expected [skywalking]
xlq20080808 opened a new issue, #11605:
URL: https://github.com/apache/skywalking/issues/11605
### Search before asking
- [X] I had searched in the [issues](https://github.com/apache/skywalking/issues?q=is%3Aissue) and found no similar issues.
### Apache SkyWalking Component
Python Agent (apache/skywalking-python)
### What happened
python version : 3.11.4
python agent : v1.0.1
flask version: 2.3.0
demo
<img width="668" alt="image" src="https://github.com/apache/skywalking/assets/33999699/838053f1-944e-44c3-bd36-6e22e6013f6d">
cross threads are not in one trace
<img width="1257" alt="image" src="https://github.com/apache/skywalking/assets/33999699/11a37da2-69f2-46da-9e22-728a1c130ba3">
### What you expected to happen
Cross threads in one trace
As shown below
<img width="1924" alt="image" src="https://github.com/apache/skywalking/assets/33999699/f4b0aad9-2c8d-4fb1-bf98-e396d2e0f5e1">
### How to reproduce
```
@app.route("/")
def hello():
thread = threading.Thread(target=post)
thread.start()
return "ok"
@runnable(op="/post")
def post():
return "post ok"
```
### Anything else
1、 when the service is started, it will try to obtain a snapshot before making method calls. The behavior is expected for an interpreted language like python.
<img width="1344" alt="image" src="https://github.com/apache/skywalking/assets/33999699/60bafc00-826c-4a19-b9ca-193af7349c61">
But when the method is actually executed, an None snapshot will cause the trace to interrupt.
<img width="1080" alt="image" src="https://github.com/apache/skywalking/assets/33999699/15fcaec8-49fb-4ba8-81c4-14db46ed849e">
2、 After checking the test cases, the writing method in the picture below can behave as expected.
Each method call triggers `snapshot = get_context().capture()`
<img width="723" alt="image" src="https://github.com/apache/skywalking/assets/33999699/065b2561-2c03-4429-ac2e-7e58b6335a6b">
But we cannot require users to write this way
### Are you willing to submit a pull request to fix on your own?
- [X] Yes I am willing to submit a pull request on my own!
### Code of Conduct
- [X] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Python agent’s @runnable does not behave as expected [skywalking]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on issue #11605:
URL: https://github.com/apache/skywalking/issues/11605#issuecomment-4231534469
## Root Cause
The `@runnable` decorator captured the snapshot at **decoration time** (`decorator()` scope), not at execution time:
```python
def decorator(func):
snapshot = get_context().capture() # runs when @runnable is applied
```
When `@runnable` is applied at **module level** (the natural Python pattern shown in the issue):
```python
@runnable(op="/post")
def post():
...
```
`get_context().capture()` runs at import time — no active trace exists, so snapshot is `None`. Later when the child thread runs, `context.continued(None)` is a no-op, and the cross-thread trace link is lost.
The existing test used `@runnable` **inline** inside a request handler, where the trace context was active at decoration time — that's why it passed.
## How to write it to work (current version)
Define and decorate the function **inside** the request handler:
```python
@app.route("/")
def hello():
@runnable(op="/post") # decorated during request, snapshot is valid
def post():
...
thread = Thread(target=post)
thread.start()
return "ok"
```
## New `sw_threading` plugin (apache/skywalking-python#390)
A new `sw_threading` plugin makes cross-thread context propagation **automatic**. No decorator needed:
```python
def post():
requests.post('http://provider:9091/users')
@app.route("/")
def hello():
thread = Thread(target=post) # snapshot captured automatically here
thread.start() # child thread gets CrossThread ref automatically
return "ok"
```
The plugin patches `Thread.__init__` (captures snapshot on parent thread) and `Thread.run` (creates local span + calls `continued()` on child thread).
`@runnable` is also updated to work with module-level definitions — it reads the snapshot from the Thread object at execution time. When `sw_threading` detects a `@runnable`-wrapped target, it defers to `@runnable` for span creation to avoid duplication.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] Python agent’s @runnable does not behave as expected [skywalking]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng closed issue #11605: [Bug] Python agent’s @runnable does not behave as expected
URL: https://github.com/apache/skywalking/issues/11605
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org