# Is it safe to keep a span alive and use its pointer across a suspension?

**URL:** <https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382>\
**Category:** Using Swift\
**Tags:** pointers, span\
**Created:** [March 17, 2026, 9:08am UTC](https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382 "2026-03-17T09:08:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [March 17, 2026, 9:08am UTC](https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382/1 "2026-03-17T09:08:57Z")

</div>

What I'm doing is the following:

```swift
func write(_ span: Span<UInt8>) async {
    let spanBufferPointer = span.withUnsafeBytes { $0 } // Get the pointer. Usually a bad pattern.
    defer { extendLifetime(span) } // Pointer must stay valid until after the suspension point.

    await withUnsafeContinuation { continuation in
        // Pointer will be used in different thread until continuation is resumed.
        scheduleWrite(spanBufferPointer, continuation) 
    }
}

```

Am I correct in thinking this is safe? Is there another solution?

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [March 17, 2026, 9:33am UTC](https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382/2 "2026-03-17T09:33:57Z")

</div>

> **[withUnsafeBytes(\_:) | Apple Developer Documentation](https://developer.apple.com/documentation/swift/span/withunsafebytes(_:))**
>
> Calls the given closure with a pointer to the underlying bytes of the viewed contiguous storage.

The buffer pointer passed as an argument to `body` is valid only during the execution of `withUnsafeBytes(_:)`. Do not store or return the pointer for later use.

---

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [March 17, 2026, 11:57am UTC](https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382/3 "2026-03-17T11:57:23Z")

</div>

I understand why it's documented that way. However, since the buffer pointer comes directly from the pointer stored in the span, I feel it should technically be fine as long as I keep the span alive. I thought it'd be good to double check though, because sometimes these unsafe constructs behave in a way I don't anticipate.

If there's a (documented) safe way to do this, I would happily use it, but I don't know of any.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [March 17, 2026, 6:43pm UTC](https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382/4 "2026-03-17T18:43:18Z")

</div>

It should be safe to use pointers from a read-only `Span` for as long as the span stays alive, so long as you don't violate the usual rules about the Span's memory (particularly that it can't be mutated while the `Span` is alive). You should be able to write this a bit more straightforwardly though by sinking the `withUnsafeBytes` into the continuation block:

```swift
func write(_ span: Span<UInt8>) async {
    defer { extendLifetime(span) } // Pointer must stay valid until after the suspension point.
    await withUnsafeContinuation { continuation in
        span.withUnsafeBytes {
          // Pointer will be used in different thread until continuation is resumed.
          scheduleWrite($0, continuation) 
        }
    }
}

```

(While not immediately relevant to your use case, in case someone finds this thread in a more general context: this is less true for `MutableSpan`, since its `withUnsafe*` accessors also assert exclusivity over the span while the pointer is in use. Since there can only be one active exclusive access to a memory location at a time, the `MutableSpan` itself would be unsafe to use while any pointers into it are potentially active.)

---

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [March 18, 2026, 11:25am UTC](https://forums.swift.org/t/is-it-safe-to-keep-a-span-alive-and-use-its-pointer-across-a-suspension/85382/5 "2026-03-18T11:25:09Z")

</div>

Good to know this should be safe, thank you! And indeed, that's a bit more straightforward. I wrote it the way I did because I thought it would be more clear that the pointer escapes.
