lambda-simulator 0.1.5

High-fidelity AWS Lambda Runtime API simulator for testing Lambda runtimes and extensions locally
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
//! Process freezing simulation for Lambda's freeze/thaw behaviour.
//!
//! This module provides the ability to simulate AWS Lambda's process freezing
//! behaviour, where the entire execution environment (runtime and all extensions)
//! is stopped (SIGSTOP) when waiting for work and resumed (SIGCONT) when an
//! invocation arrives.
//!
//! In real AWS Lambda, the entire sandbox is frozen between invocations. This
//! includes the runtime process and all extension processes. This module supports
//! freezing multiple PIDs to accurately simulate this behaviour.

use std::sync::RwLock;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use tokio::sync::Notify;

#[cfg(unix)]
use nix::sys::signal::{Signal, kill};
#[cfg(unix)]
use nix::unistd::Pid;

/// Controls whether process freezing is enabled.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FreezeMode {
    /// No process freezing (default). Fast mode for most tests.
    #[default]
    None,
    /// Send SIGSTOP/SIGCONT to simulate Lambda freeze/thaw.
    /// Only available on Unix platforms.
    Process,
}

/// Errors that can occur during freeze operations.
#[derive(Debug, Clone, PartialEq)]
pub enum FreezeError {
    /// No PIDs configured for process freezing.
    NoPidConfigured,
    /// The target process was not found.
    ProcessNotFound(i32),
    /// Permission denied to send signal.
    PermissionDenied(i32),
    /// Platform does not support process freezing.
    UnsupportedPlatform,
    /// Signal sending failed for another reason.
    SignalFailed(String),
    /// Multiple errors occurred when freezing/unfreezing multiple PIDs.
    Multiple(Vec<FreezeError>),
}

impl std::fmt::Display for FreezeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FreezeError::NoPidConfigured => write!(f, "No PIDs configured for process freezing"),
            FreezeError::ProcessNotFound(pid) => write!(f, "Process {} not found", pid),
            FreezeError::PermissionDenied(pid) => {
                write!(f, "Permission denied to send signal to process {}", pid)
            }
            FreezeError::UnsupportedPlatform => {
                write!(f, "Process freezing not supported on this platform")
            }
            FreezeError::SignalFailed(msg) => write!(f, "Signal failed: {}", msg),
            FreezeError::Multiple(errors) => {
                write!(f, "Multiple freeze errors: ")?;
                for (i, err) in errors.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", err)?;
                }
                Ok(())
            }
        }
    }
}

impl std::error::Error for FreezeError {}

/// State for managing process freezing.
///
/// Uses an epoch counter to prevent race conditions where a freeze is scheduled
/// but work arrives before it executes. The epoch is incremented on unfreeze,
/// and freeze operations check the epoch matches before proceeding.
///
/// Supports freezing multiple PIDs (runtime + extensions) to accurately simulate
/// Lambda's behaviour where the entire execution environment is frozen. PIDs can
/// be registered dynamically after creation using `register_pid()`.
pub struct FreezeState {
    /// The freeze mode (None or Process).
    mode: FreezeMode,

    /// The PIDs to freeze (runtime and extension processes).
    /// Uses RwLock for interior mutability to allow registering PIDs after creation.
    pids: RwLock<Vec<i32>>,

    /// Whether the processes are currently frozen.
    is_frozen: AtomicBool,

    /// Epoch counter for race condition prevention.
    /// Incremented on each unfreeze to invalidate pending freeze operations.
    freeze_epoch: AtomicU64,

    /// Notifier for freeze state changes.
    state_changed: Notify,
}

impl std::fmt::Debug for FreezeState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FreezeState")
            .field("mode", &self.mode)
            .field("pids", &*self.pids.read().unwrap())
            .field("is_frozen", &self.is_frozen.load(Ordering::SeqCst))
            .field("freeze_epoch", &self.freeze_epoch.load(Ordering::SeqCst))
            .finish()
    }
}

impl FreezeState {
    /// Creates a new freeze state with a single PID (for backwards compatibility).
    pub fn new(mode: FreezeMode, pid: Option<u32>) -> Self {
        Self {
            mode,
            pids: RwLock::new(pid.map(|p| vec![p as i32]).unwrap_or_default()),
            is_frozen: AtomicBool::new(false),
            freeze_epoch: AtomicU64::new(0),
            state_changed: Notify::new(),
        }
    }

    /// Creates a new freeze state with multiple PIDs.
    ///
    /// Use this to freeze the runtime and all extension processes together,
    /// which accurately simulates Lambda's freeze behaviour.
    pub fn with_pids(mode: FreezeMode, pids: Vec<u32>) -> Self {
        Self {
            mode,
            pids: RwLock::new(pids.into_iter().map(|p| p as i32).collect()),
            is_frozen: AtomicBool::new(false),
            freeze_epoch: AtomicU64::new(0),
            state_changed: Notify::new(),
        }
    }

    /// Returns the current freeze mode.
    pub fn mode(&self) -> FreezeMode {
        self.mode
    }

    /// Returns the number of registered PIDs.
    pub fn pid_count(&self) -> usize {
        self.pids.read().unwrap().len()
    }

    /// Returns the first configured PID, if any (for backwards compatibility).
    pub fn pid(&self) -> Option<i32> {
        self.pids.read().unwrap().first().copied()
    }

    /// Registers a PID to be frozen/unfrozen with the execution environment.
    ///
    /// Call this after spawning runtime or extension processes to include them
    /// in freeze/thaw operations.
    pub fn register_pid(&self, pid: u32) {
        self.pids.write().unwrap().push(pid as i32);
    }

    /// Returns whether the processes are currently frozen.
    pub fn is_frozen(&self) -> bool {
        self.is_frozen.load(Ordering::SeqCst)
    }

    /// Returns the current freeze epoch.
    pub fn current_epoch(&self) -> u64 {
        self.freeze_epoch.load(Ordering::SeqCst)
    }

    /// Waits for the freeze state to change.
    pub async fn wait_for_state_change(&self) {
        self.state_changed.notified().await;
    }

    /// Attempts to freeze all configured processes at the given epoch.
    ///
    /// The freeze will only proceed if the current epoch matches the provided epoch.
    /// This prevents race conditions where work arrives between scheduling and execution.
    ///
    /// All configured PIDs (runtime and extensions) are frozen together to simulate
    /// Lambda's behaviour where the entire execution environment is frozen.
    ///
    /// Returns Ok(true) if frozen, Ok(false) if epoch mismatch (work arrived), Err on failure.
    #[cfg(unix)]
    pub fn freeze_at_epoch(&self, epoch: u64) -> Result<bool, FreezeError> {
        if self.mode != FreezeMode::Process {
            return Ok(false);
        }

        let current_epoch = self.freeze_epoch.load(Ordering::SeqCst);
        if current_epoch != epoch {
            return Ok(false);
        }

        let pids = self.pids.read().unwrap();
        if pids.is_empty() {
            return Err(FreezeError::NoPidConfigured);
        }

        if self
            .is_frozen
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return Ok(true);
        }

        let mut errors = Vec::new();
        let mut any_success = false;

        for &pid in pids.iter() {
            match kill(Pid::from_raw(pid), Signal::SIGSTOP) {
                Ok(()) => {
                    any_success = true;
                }
                Err(nix::errno::Errno::ESRCH) => {
                    errors.push(FreezeError::ProcessNotFound(pid));
                }
                Err(nix::errno::Errno::EPERM) => {
                    errors.push(FreezeError::PermissionDenied(pid));
                }
                Err(e) => {
                    errors.push(FreezeError::SignalFailed(format!("PID {}: {}", pid, e)));
                }
            }
        }

        if !errors.is_empty() && !any_success {
            self.is_frozen.store(false, Ordering::SeqCst);
            if errors.len() == 1 {
                return Err(errors.remove(0));
            }
            return Err(FreezeError::Multiple(errors));
        }

        self.state_changed.notify_waiters();
        Ok(true)
    }

    #[cfg(not(unix))]
    pub fn freeze_at_epoch(&self, _epoch: u64) -> Result<bool, FreezeError> {
        if self.mode == FreezeMode::Process {
            Err(FreezeError::UnsupportedPlatform)
        } else {
            Ok(false)
        }
    }

    /// Unfreezes all configured processes and increments the epoch.
    ///
    /// Incrementing the epoch cancels any pending freeze operations that were
    /// scheduled before this unfreeze.
    #[cfg(unix)]
    pub fn unfreeze(&self) -> Result<(), FreezeError> {
        self.freeze_epoch.fetch_add(1, Ordering::SeqCst);

        if !self.is_frozen.swap(false, Ordering::SeqCst) {
            return Ok(());
        }

        let pids = self.pids.read().unwrap();
        if pids.is_empty() {
            return Ok(());
        }

        let mut errors = Vec::new();

        for &pid in pids.iter() {
            match kill(Pid::from_raw(pid), Signal::SIGCONT) {
                Ok(()) => {}
                Err(nix::errno::Errno::ESRCH) => {
                    errors.push(FreezeError::ProcessNotFound(pid));
                }
                Err(nix::errno::Errno::EPERM) => {
                    errors.push(FreezeError::PermissionDenied(pid));
                }
                Err(e) => {
                    errors.push(FreezeError::SignalFailed(format!("PID {}: {}", pid, e)));
                }
            }
        }

        self.state_changed.notify_waiters();

        if errors.is_empty() {
            Ok(())
        } else if errors.len() == 1 {
            Err(errors.remove(0))
        } else {
            Err(FreezeError::Multiple(errors))
        }
    }

    #[cfg(not(unix))]
    pub fn unfreeze(&self) -> Result<(), FreezeError> {
        self.freeze_epoch.fetch_add(1, Ordering::SeqCst);
        self.is_frozen.store(false, Ordering::SeqCst);
        self.state_changed.notify_waiters();
        Ok(())
    }

    /// Unconditionally unfreezes all configured processes without checking state.
    ///
    /// Used during shutdown to ensure processes aren't left frozen.
    #[cfg(unix)]
    pub fn force_unfreeze(&self) {
        self.freeze_epoch.fetch_add(1, Ordering::SeqCst);
        self.is_frozen.store(false, Ordering::SeqCst);

        let pids = self.pids.read().unwrap();
        for &pid in pids.iter() {
            let _ = kill(Pid::from_raw(pid), Signal::SIGCONT);
        }
        self.state_changed.notify_waiters();
    }

    #[cfg(not(unix))]
    pub fn force_unfreeze(&self) {
        self.freeze_epoch.fetch_add(1, Ordering::SeqCst);
        self.is_frozen.store(false, Ordering::SeqCst);
        self.state_changed.notify_waiters();
    }
}

impl Default for FreezeState {
    fn default() -> Self {
        Self::new(FreezeMode::default(), None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_freeze_mode_default_is_none() {
        assert_eq!(FreezeMode::default(), FreezeMode::None);
    }

    #[test]
    fn test_freeze_state_default() {
        let state = FreezeState::default();
        assert_eq!(state.mode(), FreezeMode::None);
        assert_eq!(state.pid(), None);
        assert_eq!(state.pid_count(), 0);
        assert!(!state.is_frozen());
        assert_eq!(state.current_epoch(), 0);
    }

    #[test]
    fn test_epoch_increments_on_unfreeze() {
        let state = FreezeState::new(FreezeMode::None, None);
        assert_eq!(state.current_epoch(), 0);

        state.unfreeze().unwrap();
        assert_eq!(state.current_epoch(), 1);

        state.unfreeze().unwrap();
        assert_eq!(state.current_epoch(), 2);
    }

    #[test]
    fn test_freeze_without_process_mode_returns_false() {
        let state = FreezeState::new(FreezeMode::None, Some(12345));
        let result = state.freeze_at_epoch(0).unwrap();
        assert!(!result);
        assert!(!state.is_frozen());
    }

    #[cfg(unix)]
    #[test]
    fn test_freeze_without_pid_returns_error() {
        let state = FreezeState::new(FreezeMode::Process, None);
        let result = state.freeze_at_epoch(0);
        assert!(matches!(result, Err(FreezeError::NoPidConfigured)));
    }

    #[test]
    fn test_epoch_mismatch_prevents_freeze() {
        let state = FreezeState::new(FreezeMode::Process, Some(12345));

        state.unfreeze().unwrap();
        assert_eq!(state.current_epoch(), 1);

        let result = state.freeze_at_epoch(0);
        assert!(matches!(result, Ok(false)));
        assert!(!state.is_frozen());
    }

    #[test]
    fn test_with_pids_creates_multi_pid_state() {
        let state = FreezeState::with_pids(FreezeMode::Process, vec![111, 222, 333]);
        assert_eq!(state.pid_count(), 3);
        assert_eq!(state.pid(), Some(111));
    }

    #[test]
    fn test_register_pid() {
        let state = FreezeState::new(FreezeMode::Process, Some(111));
        assert_eq!(state.pid_count(), 1);

        state.register_pid(222);
        assert_eq!(state.pid_count(), 2);

        state.register_pid(333);
        assert_eq!(state.pid_count(), 3);
    }
}