#2050

ecw: an upload whose destination is a directory answers 502 instead of 400

Openkavishkartha05opened 11d ago

Problem and motivation

An ECW upload (POST /files) whose destination is an existing directory answers 502 instead of a client error. #1593 fixed the read side of this and listed the write side under Not covered: "ECW uploads to a path that is a directory still answer 502 "Sandbox is waking; retry" or "Sandbox is not running". The write path shares the 409 mapping." (https://github.com/perplexityai/SPACE/pull/1593)

On current main (bd106b062):

  1. The guest maps EISDIR to FAILED_PRECONDITION "Is a directory (os error 21)" with no reason marker (crates/l40_api/spaced/guest/execution/src/filesystem/error.rs:21).
  2. tonic_status_to_ecw_error maps FAILED_PRECONDITION to 409 (crates/l40_api/data/plane/server/src/ecw_connect.rs:550).
  3. raw_file_write_error (crates/l40_api/data/plane/server/src/ecw_files.rs:1810) delegates to raw_file_read_error (ecw_files.rs:1743), which answers every 409 as EcwProxyError::sandbox_timeout(): 502 "Sandbox is not running" (crates/l40_api/data/plane/server/src/state.rs:1684).
  4. #1593 notes that, depending on the sandbox's state, the answer can instead be 502 "Sandbox is waking; retry". That rewrite only applies to 502s (ecw_runtime_access_error, crates/l40_api/data/plane/server/src/lib.rs:1561).

Both answers tell the caller the sandbox is unavailable when the request itself is wrong. A comment at state.rs:1691 records that the pinned E2B SDK turns a 502 from the envd file routes into the same TimeoutException it raises for "Sandbox is not running", so the client sees a timeout for a request that can never succeed.

E2B's envd answers this case with 400 "path is a directory: " (packages/envd/internal/api/upload.go, lines 73 to 76, in e2b-dev/infra at 6ade6f2a02). A read of a directory on SPACE ECW already answers 400 "Is a directory (os error 21)" since #1593.

Reproduction

A unit test next to #1593's a_read_refused_for_its_path_is_a_bad_request in crates/l40_api/data/plane/server/src/ecw_files/tests.rs, fed the status the guest sends for a write to a directory:

#[test]
fn repro_a_write_refused_for_its_path_is_a_bad_request() {
    let directory = raw_file_write_error(tonic::Status::failed_precondition(
        "Is a directory (os error 21)",
    ));
    assert_eq!(
        (directory.status, directory.message.as_str()),
        (StatusCode::BAD_REQUEST, "Is a directory (os error 21)"),
    );
}

cargo test -p pplx-space-data-plane-server --lib repro_a_write_refused_for_its_path_is_a_bad_request on bd106b062:

running 1 test
test ecw_files::tests::repro_a_write_refused_for_its_path_is_a_bad_request ... FAILED

failures:

---- ecw_files::tests::repro_a_write_refused_for_its_path_is_a_bad_request stdout ----

thread 'ecw_files::tests::repro_a_write_refused_for_its_path_is_a_bad_request' panicked at crates/l40_api/data/plane/server/src/ecw_files/tests.rs:1562:5:
assertion `left == right` failed
  left: (502, "Sandbox is not running")
 right: (400, "Is a directory (os error 21)")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    ecw_files::tests::repro_a_write_refused_for_its_path_is_a_bad_request

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 454 filtered out; finished in 0.00s

Expected behavior

An upload whose destination is a directory answers 400 with the guest's reason, as a read of a directory does (400 "Is a directory (os error 21)"), and as envd does. Writes refused for other reasons, and a hop's FAILED_PRECONDITION that means the sandbox cannot take the call, keep today's answers.

Proposed approach

A hop can also send an unmarked FAILED_PRECONDITION, so the write path cannot treat every one as the guest refusing the path. The read path's open handles the same ambiguity with the guest's GUEST_PATH_REFUSED marker, so I would reuse it:

  1. Guest: mark EISDIR with GUEST_PATH_REFUSED in map_io_error (filesystem/error.rs), as ENXIO and ESPIPE already are, and update the constant's doc in crates/l10_foundation/guest/status/src/lib.rs. The code stays FAILED_PRECONDITION, so Native keeps answering 412. The marker's only reader is ECW's file_read_open_error, where a directory already answers 400.
  2. Data plane: in raw_file_write_error, answer a FAILED_PRECONDITION marked GUEST_PATH_REFUSED with 400 and the guest's message, through the existing refused_file_read (renamed if it serves both). Everything else maps as today. Update the ECW limits in docs/ecw.md.

Mixed versions: the marker is additive metadata. A guest from an older release sends no marker, so its write to a directory keeps today's 502 until its template is rebuilt; nothing gets worse. An older data plane ignores the marker.

Tests: a case in filesystem/error/tests.rs for the marked EISDIR, and cases in ecw_files/tests.rs showing a marked refusal answers 400 while an unmarked FAILED_PRECONDITION and a hop's "sandbox is stopping" keep 502. Everything runs as unit tests, with no Kind cluster or live node.

Expected size: roughly 100 to 200 changed lines including tests, in two commits that each build and pass on their own: the guest marker, then the data-plane mapping with its docs.

Not covered: a Windows guest's answer for the same case, which this change does not touch.

Questions for maintainers

  1. Is reusing GUEST_PATH_REFUSED for EISDIR right, or would you rather have a separate reason value for writes?
  2. Should ENOTDIR (a path component is a file) get the same marker in this change, or stay out of scope?

Self-assessed severity

Low: the request is a client error either way, but the 502 reaches E2B SDK users as a timeout for a call that cannot succeed and hides the real reason.

I used Claude Code to draft this issue and reviewed it.

Activity

2
github-actions[bot]11d ago

Thanks for opening this issue! A SPACE maintainer will take a look.

CONTRIBUTING.md explains how we review outside contributions.

kavishkartha0511d ago

@NancyKwongPerplexity I'd like to take this one, since it's the write side you left under Not covered in #1593. The plan is to have the guest mark EISDIR with GUEST_PATH_REFUSED and have raw_file_write_error answer a marked refusal with 400, as the read path does. It's a small change of about 100 to 200 lines with tests, split into two commits: the guest marker, then the data-plane mapping and docs. Could you assign it to me, or tell me if you'd rather go another way?

I'm using Claude Code to help and will disclose it in the PR.