import asyncio
from contextlib import aclosing
from contree_client.models import EventDataStream
from contree_sdk import ContreeAsyncSession, create_sandbox_session
async def main() -> None:
async with create_sandbox_session(
image="tag:alpine:latest",
session_type=ContreeAsyncSession,
) as session:
async with asyncio.timeout(120):
await normal_interaction(session)
await child_signal(session)
await parent_finishes_first(session)
print("Completed input, signal, and parent-shutdown examples.")
async def normal_interaction(session) -> None:
print("\n--- two writes and EOF ---")
parent = await session.spawn(shell=WAIT_FOR_CHILD, disposable=True, timeout=30)
async with parent:
await wait_ready(parent)
child = await parent.run(READ_TWO_LINES, shell=True, stdin_open=True)
await parent.send_stdin("first line\n", spid=child.spid, close=False)
await parent.send_stdin("second line\n", spid=child.spid, close=True)
result = await child.wait(timeout=20)
require_success(result)
if result.stdout.as_text() != "received:first line|second line\n" or result.stderr.as_text():
raise RuntimeError("Child did not return both input lines in order")
print(result.stdout.as_text(), end="")
require_success(await parent.wait(timeout=30))
print(f"child={child.spid} exit=0; parent=SUCCESS")
async def child_signal(session) -> None:
print("\n--- signal child; parent continues ---")
parent = await session.spawn(shell=WAIT_FOR_RELEASE, disposable=True, timeout=30)
async with parent:
await wait_ready(parent)
child = await parent.run("sleep", args=["30"])
await parent.signal("SIGTERM", spid=child.spid)
result = await child.wait(timeout=20)
if result.state.signal != 15 or result.state.exit_code != -1:
raise RuntimeError("Child result did not report SIGTERM")
await release_parent(parent, RELEASE_PARENT)
require_success(await parent.wait(timeout=30))
print(f"child={child.spid} signal=15; parent=SUCCESS")
async def parent_finishes_first(session) -> None:
print("\n--- parent finishes first ---")
parent = await session.spawn(shell=WAIT_FOR_RELEASE, disposable=True, timeout=30)
async with parent:
await wait_ready(parent)
child = await parent.run(LONG_LIVED_CHILD, shell=True)
await release_parent(parent, RELEASE_AFTER_CHILD_STARTS)
require_success(await parent.wait(timeout=30))
result = await session.client.operation_subprocess(parent.uuid, child.spid)
if result.state.exit_code == 0 and result.state.signal in (-1, None, ...):
raise RuntimeError("Child unexpectedly survived parent finalization")
print(f"parent=SUCCESS; child exit={result.state.exit_code} signal={result.state.signal}")
async def wait_ready(parent) -> None:
output = ""
async with aclosing(parent.events(spid=1, timeout=20)) as events:
async for event in events:
if event.type == "stdout" and isinstance(event.data, EventDataStream):
output += event.data.as_text()
if "parent-ready\n" in output:
return
raise RuntimeError("Parent finished before reporting readiness")
async def release_parent(parent, command: str) -> None:
release = await parent.run(command, shell=True)
require_success(await release.wait(timeout=20))
def require_success(result) -> None:
if result.state.exit_code != 0 or result.state.signal not in (-1, None, ...):
raise RuntimeError(f"Unexpected process result: {result.state}")
if result.stdout.truncated is True or result.stderr.truncated is True:
raise RuntimeError("Output was truncated")
WAIT_FOR_CHILD = r"""
printf 'parent-ready\n'
i=0
while [ ! -f /tmp/child-complete ] && [ "$i" -lt 20 ]; do
i=$((i + 1))
sleep 1
done
test -f /tmp/child-complete
"""
READ_TWO_LINES = r"""
read first
read second
printf 'received:%s|%s\n' "$first" "$second"
touch /tmp/child-complete
"""
WAIT_FOR_RELEASE = r"""
printf 'parent-ready\n'
i=0
while [ ! -f /tmp/parent-release ] && [ "$i" -lt 20 ]; do
i=$((i + 1))
sleep 1
done
test -f /tmp/parent-release
"""
LONG_LIVED_CHILD = r"""
touch /tmp/child-started
sleep 30
"""
RELEASE_PARENT = "touch /tmp/parent-release"
RELEASE_AFTER_CHILD_STARTS = r"""
i=0
while [ ! -f /tmp/child-started ] && [ "$i" -lt 10 ]; do
i=$((i + 1))
sleep 1
done
test -f /tmp/child-started || exit 1
touch /tmp/parent-release
"""
asyncio.run(main())