CodeSampleX

샘플

greenlet 3.0.3: Observe the implicit parent greenlet control return behavior when a switched-to greenlet completes

검증된 샘플 — pypi greenlet 3.0.3: Observe the implicit parent greenlet control return behavior when a switched-to greenlet completes. python 3.12 · linux…

sha256:bdf2e2f5b210f6ce304b4c4a450f53972eceeab15ddf990f5cea4c215abe7355

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
3
빌드한 서명 키
3
선언된 환경 python linux x64 python python pip

검증 실행 환경

환경 컨트랙트 단계 실행일
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-17
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-08

케이스

HOW
목표
Observe the implicit parent greenlet control return behavior when a switched-to greenlet completes
패키지
심벌
  • greenlet.greenlet
  • greenlet.switch
환경
python
생성일
2026-08-17T17:46:23Z

컨트랙트

  1. When greenlet B switches to greenlet C, and C finishes its execution, control returns to C's parent (its creator), rather than back to B that performed the switch
  2. f1 starts and switches to f2 using g2.switch()
  3. f2 completes and control implicitly returns to main, not f1, leaving f1 suspended forever
  4. result contains 'main end' but never receives 'f1 end'

파일

  • NOTES.md
  • csx.json
  • requirements.txt
  • test_contract.py

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
## Search Result
`search_known_solution` for `greenlet.switch` returned NO_SAFE_MATCH (only low confidence mismatches for unrelated packages).

## Exact Release Pinned
greenlet version 3.0.3

## Observed Behaviour
A fundamental misunderstanding of asymmetric coroutines vs symmetric coroutines is the behavior of control flow upon completion.
A model typically expects `switch()` to behave like a standard coroutine `await` or `yield`, where completion of the target coroutine returns control back to the exact statement following the `switch()` call.

However, `greenlet` implements asymmetric coroutines. When `g2` is created by the main greenlet, its implicit parent is `main`. If another greenlet `g1` switches to `g2` via `g2.switch()`, when `g2` completes its function body, it implicitly returns control to its *parent* (`main`), completely bypassing `g1`.

This causes the code following `g2.switch()` in `g1` to never execute, silently leaving `g1` suspended indefinitely. The contract test proves this by recording the execution order and demonstrating that the post-switch statements in `f1` are skipped entirely.
csx.json
{"case":{"believed":"Control implicitly returns to the greenlet that called switch(), behaving like a symmetric coroutine yield or regular function call","caseId":"case:sha256:8a4e2acd34103f310cec40571fe4c6687e346f9d8b048633dc3f586314e05f07","contract":["When greenlet B switches to greenlet C, and C finishes its execution, control returns to C's parent (its creator), rather than back to B that performed the switch","f1 starts and switches to f2 using g2.switch()","f2 completes and control implicitly returns to main, not f1, leaving f1 suspended forever","result contains 'main end' but never receives 'f1 end'"],"goal":"Observe the implicit parent greenlet control return behavior when a switched-to greenlet completes","kind":"HOW","packages":["pkg:pypi/greenlet@3.0.3"],"schemaVersion":1,"symbols":["greenlet.greenlet","greenlet.switch"]},"contractCommand":["python","test_contract.py"],"environment":{"arch":"x64","ecosystem":"pypi","executionContext":"python","language":"python","os":"linux","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/greenlet@3.0.3"],"schemaVersion":1,"symbols":["greenlet.greenlet","greenlet.switch"],"verifierAdapter":"python@1"}
requirements.txt
greenlet==3.0.3
test_contract.py
import sys
import greenlet

result = []

def f2():
    result.append("f2 start")
    # finishes! Where does it return?
    # It returns to its parent, which is main, NOT f1.

def f1():
    result.append("f1 start")
    g2.switch()
    result.append("f1 end")  # This will not execute

g1 = greenlet.greenlet(f1)
g2 = greenlet.greenlet(f2)

g1.switch()
result.append("main end")

# A naive model expects ["f1 start", "f2 start", "f1 end", "main end"]
# but it actually is ["f1 start", "f2 start", "main end"]

if result != ["f1 start", "f2 start", "main end"]:
    print(f"FAILED. Result was {result}")
    sys.exit(1)
print("PASS")

오리진 시더

csx-seed