nautechsystems/nautilus_trader · error
failed to set Lighter execution context: {e}
Error message
failed to set Lighter execution context: {e} What it means
In spawn_ws_consumer (called during connect), the adapter must set the execution context on the authenticated WebSocket client — binding the account_id and account_index for account-scoped messages. If the underlying client's set_execution_context call fails, the error is wrapped with this message and connect aborts.
Source
Thrown at crates/adapters/lighter/src/execution.rs:911
// (which still owns the handler task); mirrors Hyperliquid's
// `post_ws` block.
let post_connect = async {
ws_guard
.client_mut()
.wait_until_active()
.await
.context("Lighter WebSocket did not reach active state")?;
if let Some(credential) = &self.credential {
let auth_token = build_auth_token_for(credential)
.context("failed to mint Lighter auth token")?;
let account_index = credential.account_index();
ws_guard
.client_mut()
.set_execution_context(self.core.account_id, account_index)
.await
.map_err(|e| anyhow::anyhow!("failed to set Lighter execution context: {e}"))?;
// Subscribe to the five account-scoped streams the consumption
// loop converts into typed reports. The handler merges
// `account_all_assets` and `user_stats` into a single
// AccountState (see websocket/account_state.rs).
let channels = [
LighterWsChannel::AccountAllOrders(account_index),
LighterWsChannel::AccountAllTrades(account_index),
LighterWsChannel::AccountAllPositions(account_index),
LighterWsChannel::AccountAllAssets(account_index),
LighterWsChannel::UserStats(account_index),
];
for channel in channels {
ws_guard
.client_mut()
.subscribe_account(channel.clone(), auth_token.clone())
.awaitView on GitHub (pinned to 18893faf8b)
Solutions
- Read the wrapped inner error ({e}) for the concrete WS-level cause.
- Verify account_id and account_index in your credentials are correct.
- Retry connect — transient network/endpoint failures are common.
- Check Lighter WS endpoint availability and your network/proxy setup.
- Confirm the credential/auth token was obtained before context setup (ordering issue).
Defensive patterns
Strategy: retry
Validate before calling
// validate credentials before connect assert!(!account_id.to_string().is_empty()); assert!(account_index > 0 || account_id.indicates_zero_ok());
Try / catch
match client.connect().await {
Err(e) if e.to_string().contains("failed to set Lighter execution context") => {
// inspect inner error; retry with backoff for transient WS issues
}
r => r?,
} Prevention
- Validate account_id/account_index credentials up front.
- Use retry with backoff for transient WS failures.
- Monitor venue WS endpoint availability.
When it happens
Trigger: connect() -> spawn_ws_consumer where ws_guard.client_mut().set_execution_context(...) returns an error — e.g. the WS connection is not yet established/authenticated, or the account_id/account_index is invalid for the connection.
Common situations: Network failures mid-connect; credentials with a wrong account_index; venue rejecting the session setup; WS endpoint temporarily unavailable.
Understand the failure class
Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.
Related errors
- Authentication failed: {e}
- Bybit data shutdown failed: {}
- failed to re-subscribe Lighter account channels: {error}
- Failed to send WebSocket client to handler: {e}
- Authentication failed: {e}; handler shutdown failed: {shutdo
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/9e1db877c9563a0f.
Report an issue: GitHub.