> ## Documentation Index
> Fetch the complete documentation index at: https://torqpy.itsdote.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# Shreds

> Concurrent shreds: fail-fast errors, join/wait, cancel/unspork, and ShredGroup scopes.

Returned by `Clock.spork` / module `spork`. Body exceptions **fail-fast**: the current `run` / `advance` / sim tick drive aborts after recording the failure.

```python theme={null}
from torq import Clock, Shred, ShredGroup, Join, ms, spork, shreds
```

## `Shred`

| Member | Meaning |
| - | - |
| `.index` | Creation order (same-wake tie-break) |
| `.kind` | `"gen"` or `"async"` |
| `.mode` | Live / `"done"` / `"error"` (and internal yield/advance/async states) |
| `.exception` | Body exception if the shred failed; else `None` |
| `.done` | True after normal finish or recorded body failure |
| `.failed` | True when `.exception` is set |
| `.join()` | Returns a `Join` token — gens: `yield from s.join()`; async: `await s.join()` |
| `.cancel()` | Same as `clock.unspork(self)` |
| `.cancelled` | True after cancel / unspork |

Illegal yield / non-`Dur` await raise `TypeError` from the scheduler without setting `.failed`.

## Join

`Join` parks the caller on the logical clock until the target shred finishes:

* Re-raises `target.exception` if it failed
* Cancelled targets count as done without raising
* Same-clock only; cannot join self
* No wall-clock wait from outside a shred

```python theme={null}
from torq import Clock, ms, spork

clock = Clock()

def worker():
    yield 100 * ms

def waiter(peer):
    yield from peer.join()

w = clock.spork(worker())
clock.spork(waiter(w))
clock.run()
assert w.done
```

## Cancel / unspork

| Helper | Meaning |
| - | - |
| `clock.unspork(shred)` / `shred.cancel()` | Remove from wake queue, stop gen worker / close async coro, wake joiners |
| | **Does not** clear `SimClock` hooks |

## `ShredGroup`

Fail-fast machine-local group. Spork with `group.spork(...)`.

| Form | Behavior |
| - | - |
| `async with clock.shreds() as g:` | On clean exit, `await g.join()`; on body error, cancel children |
| `with clock.shreds() as g:` | Tracks children; cancels children on exception exit; does **not** join (gens: `yield from g.join()`) |
| `g.join()` | Wait for all children; first failure cancels siblings and re-raises |
| `g.on_step` / `g.on_render` | SimClock-only hooks owned by the group; `off_*` on exit |

```python theme={null}
from torq import Clock, ms, shreds

clock = Clock()
hits = []

async def child(n):
    hits.append(n)
    await (~(50 * ms))

async def parent():
    async with shreds() as g:
        g.spork(child(1))
        g.spork(child(2))
    # children joined on clean exit

with clock:
    from torq import spork
    spork(parent)
clock.run()
```

Module `shreds()` uses the current context-bound clock.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.