Skip to content

Resolving an awaited Promise with an empty Uint8Array segfaults on windows #5226

Description

@jroweboy

What happened

Resolving an awaited Promise with an empty Uint8Array segfaults on windows

What you expected

No segfault hopefully :)

Minimal reproduction

function collectBytes(): Promise<Uint8Array> {
  return new Promise((resolve) => {
    const chunks: Uint8Array[] = [];
    process.stdin.setRawMode(true);
    process.stdin.on('data', (chunk: Uint8Array | string) => {
      chunks.push(typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk);
    });
    process.stdin.on('end', () => {
      let total = 0;
      for (const c of chunks) total += c.length;
      const out = new Uint8Array(total); // total === 0 -> empty Uint8Array
      let offset = 0;
      for (const c of chunks) { out.set(c, offset); offset += c.length; }
      resolve(out); // <- segfaults when out.length === 0
    });
    process.stdin.resume();
  });
}

async function main() {
  const bytes = await collectBytes();
  console.log('len=' + bytes.length);
}

main();

Command you ran:

(Due to #5000 which affects reading from stdin as well, I have the perry source locally, and i have to build with PERRY_WORKSPACE_ROOT)

PERRY_WORKSPACE_ROOT=/path/to/perry/ perry compile perry-bug-empty-uint8array-resolve.ts -o ./perry-bug-empty-uint8array-resolve.exe
printf '' | ./perry-bug-empty-uint8array-resolve.exe

Environment

  • Perry version: perry 0.5.1167
  • Host OS: Windows 11
  • Target: native
  • Rust toolchain (if building from source): rustc 1.96.0 (ac68faa20 2026-05-25)
  • Installed via: from source

Diagnostic output

$ printf "" | ./build/perry-bug-empty-uint8array-resolve.exe
Segmentation fault         printf "" | ./build/perry-bug-empty-uint8array-resolve.exe

Anything else

I found that just returning an empty string could work around this particular issue, but that's not a good long term plan for my usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed defect or regression

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions