stride3d/stride · error · IOException

Cannot seek to the specified position.

Error message

Cannot seek to the specified position.

What it means

VirtualFileStream.Seek validates that the resulting position stays inside the [startPosition, endPosition] window over the underlying internal stream. If the seek lands outside that window the position is reset to startPosition and an IOException is thrown, since the virtual stream is a bounded slice of a parent stream.

Solutions

  1. Clamp/validate the offset against the stream Length (endPosition - startPosition) before seeking
  2. Use SeekOrigin.Current with deltas known to stay inside the slice
  3. If unbounded access is needed, open the underlying full stream instead of the virtual slice
  4. Catch IOException and reset to a known position (the method already resets to startPosition)

Example fix

// before
slice.Seek(header.OffsetFromRealFile, SeekOrigin.Begin); // may throw
// after
var target = header.Offset - (long)slice.StartPositionWithinParent;
if (target >= 0 && target < slice.Length)
    slice.Seek(target, SeekOrigin.Begin);
Defensive patterns

Strategy: validation

Validate before calling

bool inBounds(long target) => target >= start && (end == -1 || target < end);
if (inBounds(offset)) stream.Seek(offset, SeekOrigin.Begin);

Try / catch

try { slice.Seek(offset, SeekOrigin.Begin); } catch (IOException ex) when (ex.Message == "Cannot seek to the specified position.") { slice.Seek(0, SeekOrigin.Begin); /* reset */ }

Prevention

When it happens

Trigger: Calling Seek(offset, origin) with an offset that resolves outside the slice bounds — e.g. Seek(-100, Begin) on a slice starting after 0, or seeking past endPosition.

Common situations: Random-access parsing code assuming a full file stream but given a bounded sub-stream; negative or oversized offsets; binary readers computing offsets from file headers that exceed the slice.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/ed66f72171bcb78e. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.IO/VirtualFileStream.cs:192

                break;
            case SeekOrigin.End:
                // Maybe we don't know the actual file size (full file)
                if (endPosition == -1)
                {
                    InternalStream.Seek(offset, SeekOrigin.End);
                }
                else
                {
                    InternalStream.Seek(endPosition - startPosition + offset, SeekOrigin.Begin);
                }
                break;
        }

        var newPosition = InternalStream.Position;
        if (newPosition < startPosition || (endPosition != -1 && newPosition > endPosition))
        {
            InternalStream.Position = startPosition;
            throw new IOException("Cannot seek to the specified position.");
        }

        return newPosition;
    }

    /// <inheritdoc/>
    public override void SetLength(long value)
    {
        if (endPosition != -1)
        {
            throw new NotSupportedException("Can't resize VirtualFileStream if endPosition is not -1.");
        }

        InternalStream.SetLength(value);
    }

    /// <inheritdoc/>
    /// <exception cref="IOException">The remaining capacity in the stream

View on GitHub (pinned to 96fad776d2)