PortForwarder
Reference for PortForwarder, which forwards a sandbox's TCP port, in the sandbox or on local, until it is closed.
A PortForwarder forwards one of a sandbox’s ports until it is closed. A port is on one side:
- In the sandbox (
on="sandbox", the default): a service listening in the sandbox, reached at the forwarder’slocaladdress on this machine. - On local (
on="local"): a service on this machine. The sandbox’s processes reach it at10.0.2.2:<port>, and the forwarder carries each connection tolocal.
The sandbox has to be made with the port on that side, in create_sandbox’s ports. A forwarder runs on its client’s session, so the client’s last close stops it.
See Networks and ports.
| Member | Does |
|---|---|
open |
Forwards one of the sandbox’s ports, on the side on. Returns once it is open. |
port, on, local |
The port, its side, and its local end as host:port. |
last_error |
Why the last connection that failed did; none again once a later connection reaches the port. |
wait_closed |
Waits until it has ended, and returns why. |
close |
Stops forwarding; the sandbox keeps its port. |
open
Forwards the port port of sandbox, one it was made with on the side on, and returns once it is open.
local, as host:port, is its local end:
- For a port in the sandbox: the address it is reached at. Without it, a free port on
127.0.0.1, or, from a server on this machine, the address the server returned for the port. - For a port on local: where its connections are carried,
127.0.0.1:<port>without it.
A port on local that the sandbox wasn’t made with is refused with 404.
In Python, open is awaited, and the forwarder is a context manager: async with await PortForwarder.open(sandbox, 8000) as forwarder:.
@classmethodasync def open( sandbox: Sandbox, port: int, *, on: PortSide = "sandbox", local: str | None = None) -> PortForwarderstatic open( sandbox: Sandbox, port: number, options?: { on?: PortSide; local?: string },): Promise<PortForwarder>pub fn open( sandbox: &Sandbox, port: u16, on: PortSide, local: Option<&str>,) -> Call<PortForwarder>PortSide is "sandbox" or "local"; in Rust, PortSide::Sandbox or PortSide::Local.
port, on, local
The port, its side, and its local end as host:port: for a port in the sandbox, the address it is reached at; for a port on local, where its connections are carried.
port: inton: PortSidelocal: strreadonly port: numberreadonly on: PortSidereadonly local: stringpub fn port(&self) -> u16pub fn on(&self) -> PortSidepub fn local(&self) -> &strlast_error
Why the last connection through the forwarder that failed did: its WebSocket refused, closed before the port sent a byte, or its target not reached, with the server’s reason if it gave one. The message names the port, and status is the server’s when it refused the WebSocket. It is none until a connection fails, and none again once a later connection reaches the port. Each failure is also logged as a warning.
A port in the sandbox reached at a server’s own address, its url, goes through no forwarder, so it has no last_error.
@property last_error: VirtxError | Nonereadonly lastError: VirtxError | nullpub fn last_error(&self) -> Option<Error>wait_closed
Waits until the forwarder has ended, and returns the error that ended it: for a port on local, 404 once its sandbox is gone. A port in the sandbox is forwarded until it is closed.
async def wait_closed() -> VirtxErrorwaitClosed(): Promise<VirtxError>pub fn wait_closed(&self) -> Closed // awaited, or .wait() without a runtime: Errorclose
Stops forwarding; the sandbox keeps its port, and another forwarder can open it again. Closing twice does nothing. Rust has no close: dropping the forwarder stops it.
def close() -> Noneclose(): void[Symbol.dispose](): voiddrop(forwarder) // no close method: dropping the PortForwarder stops forwardingErrors
A call that fails raises a VirtxError in Python, throws one in Node.js, and returns one in Rust. Branch on its status and code, not its message. These are the errors the calls on this page return most often.
| Error | From | Cause |
|---|---|---|
404 |
open with on="local" |
The sandbox wasn’t made with this port on local. Create it with the port in ports, on local. |
404 |
wait_closed, as the value it returns |
The sandbox of a port on local is gone. |
A connection closes at once, and last_error is set |
Connections through the forwarder | Nothing listens on the port in the sandbox yet, or, for a port on local, nothing listens at local. Start the service first, then connect again. |
Any call can also fail with 401, unauthorized or 403, forbidden (see Client), or with source transport when the connection drops. The client retries 503, capacity and a 429, quota for the request rate by itself for up to 30 seconds: see Retries.