traverse-runtime 0.10.1

Core execution engine for the Traverse capability runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Ignored stress tests for `ThreadPoolExecutor`.
//!
//! Governed by spec `047-thread-pool-executor`.

use std::error::Error;
use std::process::Command;
use std::sync::{
    Arc, Barrier, Mutex, PoisonError,
    atomic::{AtomicUsize, Ordering},
};
use std::thread;
use std::time::{Duration, Instant};

use serde_json::{Value, json};
use traverse_runtime::executor::{
    ArtifactType, CapabilityExecutor, ExecutorCapability, ExecutorError, NativeExecutor,
    ThreadPoolExecutor, ThreadPoolExecutorConfig,
};

type TestResult<T> = Result<T, Box<dyn Error>>;

fn native_capability() -> ExecutorCapability {
    ExecutorCapability {
        capability_id: "stress.thread_pool.native".to_string(),
        artifact_type: ArtifactType::Native,
        wasm_binary_path: None,
        wasm_checksum: None,
        host_abi_version: None,
        emits: Vec::new(),
        service_type: traverse_contracts::ServiceType::Stateless,
    }
}

fn executor(
    capacity: usize,
    handler: impl Fn(&Value) -> Result<Value, String> + Send + Sync + 'static,
) -> TestResult<ThreadPoolExecutor> {
    Ok(ThreadPoolExecutor::new(
        ThreadPoolExecutorConfig { capacity },
        Box::new(NativeExecutor::new(handler)),
    )?)
}

fn execute(executor: &ThreadPoolExecutor, input: &Value) -> Result<Value, ExecutorError> {
    executor
        .execute(&native_capability(), input)
        .map(|output| output.value)
}

fn lock_errors(errors: &Mutex<Vec<String>>) -> std::sync::MutexGuard<'_, Vec<String>> {
    errors.lock().unwrap_or_else(PoisonError::into_inner)
}

fn configured_duration(default_seconds: u64) -> Duration {
    std::env::var("TRAVERSE_STRESS_SECONDS")
        .ok()
        .and_then(|raw| raw.parse::<u64>().ok())
        .map_or_else(|| Duration::from_secs(default_seconds), Duration::from_secs)
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
fn parse_first_number(raw: &[u8]) -> Option<u64> {
    String::from_utf8_lossy(raw)
        .split_whitespace()
        .find_map(|part| part.trim().parse::<u64>().ok())
}

#[cfg(target_os = "linux")]
fn current_rss_kb() -> Option<u64> {
    let status = std::fs::read_to_string("/proc/self/status").ok()?;
    status
        .lines()
        .find(|line| line.starts_with("VmRSS:"))
        .and_then(|line| line.split_whitespace().nth(1))
        .and_then(|value| value.parse::<u64>().ok())
}

#[cfg(target_os = "macos")]
fn current_rss_kb() -> Option<u64> {
    let pid = std::process::id().to_string();
    let output = Command::new("ps")
        .args(["-o", "rss=", "-p", &pid])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    parse_first_number(&output.stdout)
}

#[cfg(target_os = "windows")]
fn current_rss_kb() -> Option<u64> {
    let pid = std::process::id().to_string();
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-Command",
            &format!("(Get-Process -Id {pid}).WorkingSet64"),
        ])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    parse_first_number(&output.stdout).map(|bytes| bytes / 1024)
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
fn current_rss_kb() -> Option<u64> {
    None
}

#[cfg(target_os = "linux")]
fn current_fd_or_handle_count() -> Option<u64> {
    Some(std::fs::read_dir("/proc/self/fd").ok()?.count() as u64)
}

#[cfg(target_os = "macos")]
fn current_fd_or_handle_count() -> Option<u64> {
    let pid = std::process::id().to_string();
    let output = Command::new("lsof").args(["-p", &pid]).output().ok()?;
    if !output.status.success() {
        return None;
    }
    Some(
        String::from_utf8_lossy(&output.stdout)
            .lines()
            .skip(1)
            .count() as u64,
    )
}

#[cfg(target_os = "windows")]
fn current_fd_or_handle_count() -> Option<u64> {
    let pid = std::process::id().to_string();
    let output = Command::new("powershell")
        .args([
            "-NoProfile",
            "-Command",
            &format!("(Get-Process -Id {pid}).HandleCount"),
        ])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    parse_first_number(&output.stdout)
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
fn current_fd_or_handle_count() -> Option<u64> {
    None
}

/// Measured create-use-drop cycles in `stress_no_fd_leak`. A real per-cycle
/// leak grows the descriptor count by hundreds over this many cycles, far
/// beyond `FD_LEAK_TOLERANCE`.
const FD_LEAK_CYCLES: u64 = 1_000;

/// Cycles run before the baseline is captured, so lazy process-wide
/// initialization (stdio, threading, platform probes) opens its descriptors
/// outside the measured window.
const FD_LEAK_WARMUP_CYCLES: u64 = 50;

/// Allowance for descriptor churn unrelated to the executor (test-harness
/// threads, background activity on shared CI runners). Must stay far below
/// the growth a genuine per-cycle leak produces over `FD_LEAK_CYCLES`.
const FD_LEAK_TOLERANCE: u64 = 8;

/// Samples taken per measurement; the minimum filters out descriptors that
/// unrelated runner activity opens transiently during sampling.
const FD_SAMPLES_PER_MEASUREMENT: u32 = 5;

/// Re-measurements allowed before the growth assertion fails, giving
/// transient descriptor churn time to settle.
const FD_MEASUREMENT_RETRIES: u32 = 3;

fn run_fd_leak_cycles(cycles: std::ops::Range<u64>) -> TestResult<()> {
    for i in cycles {
        let executor = executor(2, |input| Ok(input.clone()))?;
        let input = json!({ "cycle": i });
        let output = execute(&executor, &input)?;
        assert_eq!(output, input);
    }
    Ok(())
}

fn stable_fd_or_handle_count() -> Option<u64> {
    let mut stable: Option<u64> = None;
    for sample in 0..FD_SAMPLES_PER_MEASUREMENT {
        if sample > 0 {
            thread::sleep(Duration::from_millis(100));
        }
        if let Some(count) = current_fd_or_handle_count() {
            stable = Some(stable.map_or(count, |current| current.min(count)));
        }
    }
    stable
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_50_threads_burst() -> TestResult<()> {
    let executor = Arc::new(executor(8, |input| Ok(input.clone()))?);
    let barrier = Arc::new(Barrier::new(51));
    let errors = Arc::new(Mutex::new(Vec::new()));
    let start = Instant::now();

    thread::scope(|scope| {
        for i in 0_u64..50 {
            let executor = Arc::clone(&executor);
            let barrier = Arc::clone(&barrier);
            let errors = Arc::clone(&errors);
            scope.spawn(move || {
                barrier.wait();
                let expected = json!({ "i": i });
                match execute(&executor, &expected) {
                    Ok(output) if output == expected => {}
                    Ok(output) => lock_errors(&errors).push(format!("wrong output {output:?}")),
                    Err(err) => lock_errors(&errors).push(format!("executor error {err:?}")),
                }
            });
        }
        barrier.wait();
    });

    let elapsed = start.elapsed();
    let errors = lock_errors(&errors);
    assert!(errors.is_empty(), "burst errors: {errors:?}");
    assert!(elapsed < Duration::from_secs(5), "burst took {elapsed:?}");
    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_10k_sequential_calls() -> TestResult<()> {
    let executor = executor(4, |input| Ok(input.clone()))?;

    for i in 0_u64..10_000 {
        let expected = json!({ "i": i });
        let output = execute(&executor, &expected)?;
        assert_eq!(output, expected);
    }

    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_mixed_success_and_error() -> TestResult<()> {
    let executor = executor(8, |input| {
        if input.get("should_error").and_then(Value::as_bool) == Some(true) {
            return Err("deliberate stress error".to_string());
        }
        Ok(input.clone())
    })?;

    let success_count = AtomicUsize::new(0);
    let error_count = AtomicUsize::new(0);

    for i in 0_u64..1_000 {
        let should_error = i % 2 == 1;
        let input = json!({ "i": i, "should_error": should_error });
        match execute(&executor, &input) {
            Ok(output) if !should_error && output == input => {
                success_count.fetch_add(1, Ordering::SeqCst);
            }
            Err(ExecutorError::ExecutionFailed(msg))
                if should_error && msg.contains("deliberate stress error") =>
            {
                error_count.fetch_add(1, Ordering::SeqCst);
            }
            other => return Err(format!("unexpected mixed result for {i}: {other:?}").into()),
        }
    }

    assert_eq!(success_count.load(Ordering::SeqCst), 500);
    assert_eq!(error_count.load(Ordering::SeqCst), 500);
    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_interleaved_panic_and_success() -> TestResult<()> {
    let executor = executor(4, |input| {
        assert!(
            input.get("panic").and_then(Value::as_bool) != Some(true),
            "deliberate stress panic"
        );
        Ok(input.clone())
    })?;

    let success_count = AtomicUsize::new(0);
    let panic_count = AtomicUsize::new(0);

    for i in 0_u64..200 {
        let should_panic = i % 5 == 0;
        let input = json!({ "i": i, "panic": should_panic });
        match execute(&executor, &input) {
            Ok(output) if !should_panic && output == input => {
                success_count.fetch_add(1, Ordering::SeqCst);
            }
            Err(ExecutorError::ExecutionFailed(msg))
                if should_panic && msg.contains("panicked") =>
            {
                panic_count.fetch_add(1, Ordering::SeqCst);
            }
            other => return Err(format!("unexpected panic mix result for {i}: {other:?}").into()),
        }
    }

    let final_input = json!({ "after": "panic" });
    let final_output = execute(&executor, &final_input)?;
    assert_eq!(final_output, json!({ "after": "panic" }));
    assert_eq!(success_count.load(Ordering::SeqCst), 160);
    assert_eq!(panic_count.load(Ordering::SeqCst), 40);
    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_memory_stable_60s() -> TestResult<()> {
    let executor = Arc::new(executor(4, |input| Ok(input.clone()))?);
    let duration = configured_duration(60);
    let deadline = Instant::now() + duration;
    let errors = Arc::new(Mutex::new(Vec::new()));
    let calls = Arc::new(AtomicUsize::new(0));
    let baseline = current_rss_kb();
    let mut samples = Vec::new();

    thread::scope(|scope| {
        for worker in 0_u64..4 {
            let executor = Arc::clone(&executor);
            let errors = Arc::clone(&errors);
            let calls = Arc::clone(&calls);
            scope.spawn(move || {
                let mut iteration = 0_u64;
                while Instant::now() < deadline {
                    let input = json!({ "worker": worker, "iteration": iteration });
                    match execute(&executor, &input) {
                        Ok(output) if output == input => {
                            calls.fetch_add(1, Ordering::SeqCst);
                        }
                        Ok(output) => lock_errors(&errors).push(format!("wrong output {output:?}")),
                        Err(err) => lock_errors(&errors).push(format!("executor error {err:?}")),
                    }
                    iteration = iteration.saturating_add(1);
                }
            });
        }

        while Instant::now() < deadline {
            thread::sleep(Duration::from_secs(5).min(duration));
            if let Some(sample) = current_rss_kb() {
                samples.push(sample);
            }
        }
    });

    let errors = lock_errors(&errors);
    assert!(errors.is_empty(), "memory stress errors: {errors:?}");
    assert!(
        calls.load(Ordering::SeqCst) > 0,
        "memory stress made no executor calls"
    );

    if let Some(baseline) = baseline {
        let peak = samples.iter().copied().max().unwrap_or(baseline);
        let allowed = baseline + (baseline / 10) + 8 * 1024;
        assert!(
            peak <= allowed,
            "RSS grew beyond threshold: baseline={baseline}KiB peak={peak}KiB allowed={allowed}KiB"
        );
    }

    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_no_fd_leak() -> TestResult<()> {
    run_fd_leak_cycles(0..FD_LEAK_WARMUP_CYCLES)?;
    let Some(baseline) = stable_fd_or_handle_count() else {
        return Ok(());
    };

    run_fd_leak_cycles(FD_LEAK_WARMUP_CYCLES..FD_LEAK_WARMUP_CYCLES + FD_LEAK_CYCLES)?;

    let allowed = baseline + FD_LEAK_TOLERANCE;
    let mut after = None;
    for attempt in 0..=FD_MEASUREMENT_RETRIES {
        if attempt > 0 {
            thread::sleep(Duration::from_millis(500));
        }
        after = stable_fd_or_handle_count();
        match after {
            Some(count) if count > allowed => {}
            _ => break,
        }
    }

    if let Some(after) = after {
        assert!(
            after <= allowed,
            "file descriptor or handle count grew beyond tolerance over \
             {FD_LEAK_CYCLES} create-use-drop cycles: \
             baseline={baseline} after={after} allowed={allowed}"
        );
    }

    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_capacity_1_no_deadlock() -> TestResult<()> {
    let executor = executor(1, |input| Ok(input.clone()))?;
    let start = Instant::now();

    for i in 0_u64..100 {
        let input = json!({ "i": i });
        let output = execute(&executor, &input)?;
        assert_eq!(output, json!({ "i": i }));
    }

    assert!(
        start.elapsed() < Duration::from_secs(10),
        "capacity-1 stress exceeded timeout"
    );
    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn stress_capacity_256_max() -> TestResult<()> {
    let executor = Arc::new(executor(256, |input| Ok(input.clone()))?);
    let barrier = Arc::new(Barrier::new(257));
    let errors = Arc::new(Mutex::new(Vec::new()));

    thread::scope(|scope| {
        for i in 0_u64..256 {
            let executor = Arc::clone(&executor);
            let barrier = Arc::clone(&barrier);
            let errors = Arc::clone(&errors);
            scope.spawn(move || {
                barrier.wait();
                let expected = json!({ "i": i });
                match execute(&executor, &expected) {
                    Ok(output) if output == expected => {}
                    Ok(output) => lock_errors(&errors).push(format!("wrong output {output:?}")),
                    Err(err) => lock_errors(&errors).push(format!("executor error {err:?}")),
                }
            });
        }
        barrier.wait();
    });

    let errors = lock_errors(&errors);
    assert!(errors.is_empty(), "capacity-256 errors: {errors:?}");
    if let Some(rss) = current_rss_kb() {
        assert!(rss < 512 * 1024, "RSS exceeded 512MiB: {rss}KiB");
    }
    Ok(())
}

#[ignore = "runs only in the dedicated ThreadPoolExecutor stress CI job"]
#[test]
fn wasm_build_not_broken() -> TestResult<()> {
    let target = "wasm32-wasip1";
    let target_libdir = Command::new("rustc")
        .args(["--print", "target-libdir", "--target", target])
        .output()
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .map(|path| path.trim().to_string());

    let Some(target_libdir) = target_libdir else {
        eprintln!("Skipping WASM build stress check: unable to resolve {target} target libdir");
        return Ok(());
    };

    if !std::path::Path::new(&target_libdir)
        .join("libcore.rlib")
        .is_file()
    {
        eprintln!("Skipping WASM build stress check: {target} stdlib is not installed");
        return Ok(());
    }

    let status = Command::new("cargo")
        .args([
            "build",
            "--target",
            target,
            "-p",
            "traverse-expedition-wasm",
        ])
        .status()?;

    assert!(
        status.success(),
        "WASM build failed for traverse-expedition-wasm target {target}"
    );
    Ok(())
}