rmux-server 0.10.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use super::*;

use super::super::lock_support::{install_lock_identity_pause, LockIdentityPausePoint};

async fn create_session(handler: &RequestHandler, session: &SessionName) {
    let response = handler
        .handle(Request::NewSession(NewSessionRequest {
            session_name: session.clone(),
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
        }))
        .await;
    assert!(matches!(response, Response::NewSession(_)), "{response:?}");
}

async fn set_lock_command(handler: &RequestHandler) {
    let response = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::LockCommand,
            value: "lock-identity-probe".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(response, Response::SetOption(_)), "{response:?}");
}

async fn rename_session(
    handler: &RequestHandler,
    current_name: SessionName,
    new_name: SessionName,
) {
    let response = handler
        .handle(Request::RenameSession(RenameSessionRequest {
            target: current_name,
            new_name,
        }))
        .await;
    assert!(
        matches!(response, Response::RenameSession(_)),
        "{response:?}"
    );
}

async fn switch_attached_client(handler: &RequestHandler, attach_pid: u32, target: SessionName) {
    let response = handler
        .dispatch(
            attach_pid,
            Request::SwitchClient(SwitchClientRequest {
                target: target.clone(),
            }),
        )
        .await
        .response;
    assert_eq!(
        response,
        Response::SwitchClient(rmux_proto::SwitchClientResponse {
            session_name: target,
        })
    );
}

async fn expect_lock_control(
    control_rx: &mut mpsc::UnboundedReceiver<AttachControl>,
    context: &str,
) {
    tokio::time::timeout(Duration::from_secs(2), async {
        while let Some(control) = control_rx.recv().await {
            if matches!(control, AttachControl::LockShellCommand(_)) {
                return;
            }
        }
        panic!("attach control channel closed while waiting for {context}");
    })
    .await
    .unwrap_or_else(|_| panic!("timed out waiting for {context}"));
}

fn assert_replacement_is_unlocked(
    active_attach: &super::super::attach_support::ActiveAttachState,
    attach_pid: u32,
    replacement_id: u64,
    control_rx: &mut mpsc::UnboundedReceiver<AttachControl>,
) {
    while let Ok(control) = control_rx.try_recv() {
        assert!(
            !matches!(control, AttachControl::LockShellCommand(_)),
            "replacement attach must not receive the stale lock command"
        );
    }
    let replacement = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("replacement attach survives");
    assert_eq!(replacement.id, replacement_id);
    assert!(!replacement.suspended);
}

fn assert_attach_is_unlocked(
    active_attach: &super::super::attach_support::ActiveAttachState,
    attach_pid: u32,
    expected_session: &SessionName,
    control_rx: &mut mpsc::UnboundedReceiver<AttachControl>,
) {
    while let Ok(control) = control_rx.try_recv() {
        assert!(
            !matches!(control, AttachControl::LockShellCommand(_)),
            "attach outside the lock-session target must not receive a lock command"
        );
    }
    let active = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("attach survives");
    assert_eq!(&active.session_name, expected_session);
    assert!(!active.suspended);
}

#[tokio::test]
async fn lock_server_does_not_suspend_same_pid_attach_replacement_after_snapshot() {
    let handler = RequestHandler::new();
    let session = session_name("lock-server-attach-identity");
    let attach_pid = 920_060;
    create_session(&handler, &session).await;
    set_lock_command(&handler).await;

    let (old_tx, _old_rx) = mpsc::unbounded_channel();
    let old_id = handler
        .register_attach(attach_pid, session.clone(), old_tx)
        .await;
    let pause = install_lock_identity_pause(LockIdentityPausePoint::ServerClient(attach_pid));
    let lock_handler = handler.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockServer(rmux_proto::LockServerRequest))
            .await
    });

    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-server captures attached-client identities");
    let (replacement_tx, mut replacement_rx) = mpsc::unbounded_channel();
    let replacement_id = handler
        .register_attach(attach_pid, session, replacement_tx)
        .await;
    assert_ne!(replacement_id, old_id);
    pause.release();

    let response = lock.await.expect("lock-server task joins");
    assert!(matches!(response, Response::LockServer(_)), "{response:?}");
    let active_attach = handler.active_attach.lock().await;
    assert_replacement_is_unlocked(
        &active_attach,
        attach_pid,
        replacement_id,
        &mut replacement_rx,
    );
}

#[tokio::test]
async fn lock_server_follows_exact_session_identity_across_rename() {
    let handler = RequestHandler::new();
    let original_name = session_name("lock-server-before-rename");
    let renamed = session_name("lock-server-after-rename");
    let attach_pid = 920_064;
    create_session(&handler, &original_name).await;
    set_lock_command(&handler).await;
    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let attach_id = handler
        .register_attach(attach_pid, original_name.clone(), control_tx)
        .await;
    let session_id = handler
        .active_attach_identity_for_test(attach_pid)
        .await
        .session_id();

    let pause = install_lock_identity_pause(LockIdentityPausePoint::ServerClient(attach_pid));
    let lock_handler = handler.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockServer(rmux_proto::LockServerRequest))
            .await
    });
    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-server captures the exact attach identity");
    rename_session(&handler, original_name, renamed.clone()).await;
    pause.release();

    let response = lock.await.expect("lock-server task joins");
    assert!(matches!(response, Response::LockServer(_)), "{response:?}");
    expect_lock_control(&mut control_rx, "lock-server after rename").await;
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("attach survives");
    assert_eq!(active.id, attach_id);
    assert_eq!(active.session_id, session_id);
    assert_eq!(active.session_name, renamed);
    assert!(active.suspended);
}

#[tokio::test]
async fn lock_server_follows_same_attach_registration_across_session_switch() {
    let handler = RequestHandler::new();
    let alpha = session_name("lock-server-before-switch");
    let beta = session_name("lock-server-after-switch");
    let attach_pid = 920_067;
    create_session(&handler, &alpha).await;
    create_session(&handler, &beta).await;
    set_lock_command(&handler).await;
    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let attach_id = handler.register_attach(attach_pid, alpha, control_tx).await;

    let pause = install_lock_identity_pause(LockIdentityPausePoint::ServerClient(attach_pid));
    let lock_handler = handler.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockServer(rmux_proto::LockServerRequest))
            .await
    });
    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-server captures the attach registration");
    switch_attached_client(&handler, attach_pid, beta.clone()).await;
    pause.release();

    let response = lock.await.expect("lock-server task joins");
    assert!(matches!(response, Response::LockServer(_)), "{response:?}");
    expect_lock_control(&mut control_rx, "lock-server after session switch").await;
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("attach survives");
    assert_eq!(active.id, attach_id);
    assert_eq!(active.session_name, beta);
    assert!(active.suspended);
}

#[tokio::test]
async fn lock_client_does_not_suspend_same_pid_attach_replacement_after_resolution() {
    let handler = RequestHandler::new();
    let session = session_name("lock-client-attach-identity");
    let attach_pid = 920_061;
    create_session(&handler, &session).await;
    set_lock_command(&handler).await;

    let (old_tx, _old_rx) = mpsc::unbounded_channel();
    let old_id = handler
        .register_attach(attach_pid, session.clone(), old_tx)
        .await;
    let pause = install_lock_identity_pause(LockIdentityPausePoint::Client(attach_pid));
    let lock_handler = handler.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .dispatch(
                attach_pid,
                Request::LockClient(rmux_proto::LockClientRequest {
                    target_client: "=".to_owned(),
                }),
            )
            .await
            .response
    });

    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-client captures the attached-client identity");
    let (replacement_tx, mut replacement_rx) = mpsc::unbounded_channel();
    let replacement_id = handler
        .register_attach(attach_pid, session, replacement_tx)
        .await;
    assert_ne!(replacement_id, old_id);
    pause.release();

    let response = lock.await.expect("lock-client task joins");
    assert!(matches!(response, Response::LockClient(_)), "{response:?}");
    let active_attach = handler.active_attach.lock().await;
    assert_replacement_is_unlocked(
        &active_attach,
        attach_pid,
        replacement_id,
        &mut replacement_rx,
    );
}

#[tokio::test]
async fn lock_client_follows_exact_session_identity_across_rename() {
    let handler = RequestHandler::new();
    let original_name = session_name("lock-client-before-rename");
    let renamed = session_name("lock-client-after-rename");
    let attach_pid = 920_065;
    create_session(&handler, &original_name).await;
    set_lock_command(&handler).await;
    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let attach_id = handler
        .register_attach(attach_pid, original_name.clone(), control_tx)
        .await;
    let session_id = handler
        .active_attach_identity_for_test(attach_pid)
        .await
        .session_id();

    let pause = install_lock_identity_pause(LockIdentityPausePoint::Client(attach_pid));
    let lock_handler = handler.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .dispatch(
                attach_pid,
                Request::LockClient(rmux_proto::LockClientRequest {
                    target_client: "=".to_owned(),
                }),
            )
            .await
            .response
    });
    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-client captures the exact attach identity");
    rename_session(&handler, original_name, renamed.clone()).await;
    pause.release();

    let response = lock.await.expect("lock-client task joins");
    assert!(matches!(response, Response::LockClient(_)), "{response:?}");
    expect_lock_control(&mut control_rx, "lock-client after rename").await;
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("attach survives");
    assert_eq!(active.id, attach_id);
    assert_eq!(active.session_id, session_id);
    assert_eq!(active.session_name, renamed);
    assert!(active.suspended);
}

#[tokio::test]
async fn lock_client_follows_same_attach_registration_across_session_switch() {
    let handler = RequestHandler::new();
    let alpha = session_name("lock-client-before-switch");
    let beta = session_name("lock-client-after-switch");
    let attach_pid = 920_068;
    create_session(&handler, &alpha).await;
    create_session(&handler, &beta).await;
    set_lock_command(&handler).await;
    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let attach_id = handler.register_attach(attach_pid, alpha, control_tx).await;

    let pause = install_lock_identity_pause(LockIdentityPausePoint::Client(attach_pid));
    let lock_handler = handler.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .dispatch(
                attach_pid,
                Request::LockClient(rmux_proto::LockClientRequest {
                    target_client: "=".to_owned(),
                }),
            )
            .await
            .response
    });
    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-client captures the attach registration");
    switch_attached_client(&handler, attach_pid, beta.clone()).await;
    pause.release();

    let response = lock.await.expect("lock-client task joins");
    assert!(matches!(response, Response::LockClient(_)), "{response:?}");
    expect_lock_control(&mut control_rx, "lock-client after session switch").await;
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("attach survives");
    assert_eq!(active.id, attach_id);
    assert_eq!(active.session_name, beta);
    assert!(active.suspended);
}

#[tokio::test]
async fn lock_session_does_not_suspend_same_pid_attach_replacement_after_snapshot() {
    let handler = RequestHandler::new();
    let session = session_name("lock-session-attach-identity");
    let attach_pid = 920_062;
    create_session(&handler, &session).await;
    set_lock_command(&handler).await;

    let (old_tx, _old_rx) = mpsc::unbounded_channel();
    let old_id = handler
        .register_attach(attach_pid, session.clone(), old_tx)
        .await;
    let pause =
        install_lock_identity_pause(LockIdentityPausePoint::SessionClients(session.clone()));
    let lock_handler = handler.clone();
    let lock_target = session.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockSession(rmux_proto::LockSessionRequest {
                target: lock_target,
            }))
            .await
    });

    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-session captures attached-client identities");
    let (replacement_tx, mut replacement_rx) = mpsc::unbounded_channel();
    let replacement_id = handler
        .register_attach(attach_pid, session, replacement_tx)
        .await;
    assert_ne!(replacement_id, old_id);
    pause.release();

    let response = lock.await.expect("lock-session task joins");
    assert!(matches!(response, Response::LockSession(_)), "{response:?}");
    let active_attach = handler.active_attach.lock().await;
    assert_replacement_is_unlocked(
        &active_attach,
        attach_pid,
        replacement_id,
        &mut replacement_rx,
    );
}

#[tokio::test]
async fn lock_session_follows_exact_session_identity_across_rename() {
    let handler = RequestHandler::new();
    let original_name = session_name("lock-session-before-rename");
    let renamed = session_name("lock-session-after-rename");
    let attach_pid = 920_066;
    create_session(&handler, &original_name).await;
    set_lock_command(&handler).await;
    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let attach_id = handler
        .register_attach(attach_pid, original_name.clone(), control_tx)
        .await;
    let session_id = handler
        .active_attach_identity_for_test(attach_pid)
        .await
        .session_id();

    let pause = install_lock_identity_pause(LockIdentityPausePoint::Session(original_name.clone()));
    let lock_handler = handler.clone();
    let lock_target = original_name.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockSession(rmux_proto::LockSessionRequest {
                target: lock_target,
            }))
            .await
    });
    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-session captures the exact session identity");
    rename_session(&handler, original_name, renamed.clone()).await;
    pause.release();

    let response = lock.await.expect("lock-session task joins");
    assert!(matches!(response, Response::LockSession(_)), "{response:?}");
    expect_lock_control(&mut control_rx, "lock-session after rename").await;
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&attach_pid)
        .expect("attach survives");
    assert_eq!(active.id, attach_id);
    assert_eq!(active.session_id, session_id);
    assert_eq!(active.session_name, renamed);
    assert!(active.suspended);
}

#[tokio::test]
async fn lock_session_does_not_follow_attach_that_switches_out_of_target_session() {
    let handler = RequestHandler::new();
    let alpha = session_name("lock-session-before-switch");
    let beta = session_name("lock-session-after-switch");
    let attach_pid = 920_069;
    create_session(&handler, &alpha).await;
    create_session(&handler, &beta).await;
    set_lock_command(&handler).await;
    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    handler
        .register_attach(attach_pid, alpha.clone(), control_tx)
        .await;

    let pause = install_lock_identity_pause(LockIdentityPausePoint::SessionClients(alpha.clone()));
    let lock_handler = handler.clone();
    let lock_target = alpha;
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockSession(rmux_proto::LockSessionRequest {
                target: lock_target,
            }))
            .await
    });
    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-session captures target-session attach registrations");
    switch_attached_client(&handler, attach_pid, beta.clone()).await;
    pause.release();

    let response = lock.await.expect("lock-session task joins");
    assert!(matches!(response, Response::LockSession(_)), "{response:?}");
    let active_attach = handler.active_attach.lock().await;
    assert_attach_is_unlocked(&active_attach, attach_pid, &beta, &mut control_rx);
}

#[tokio::test]
async fn lock_session_does_not_suspend_attach_in_same_name_recreated_session() {
    let handler = RequestHandler::new();
    let session = session_name("lock-session-recreated-identity");
    let keeper = session_name("lock-session-recreated-keeper");
    let replacement_pid = 920_063;
    create_session(&handler, &session).await;
    create_session(&handler, &keeper).await;
    set_lock_command(&handler).await;
    let original_session_id = handler
        .state
        .lock()
        .await
        .sessions
        .session(&session)
        .expect("original session exists")
        .id();

    let pause = install_lock_identity_pause(LockIdentityPausePoint::Session(session.clone()));
    let lock_handler = handler.clone();
    let lock_target = session.clone();
    let lock = tokio::spawn(async move {
        lock_handler
            .handle(Request::LockSession(rmux_proto::LockSessionRequest {
                target: lock_target,
            }))
            .await
    });

    tokio::time::timeout(Duration::from_secs(2), pause.wait_until_reached())
        .await
        .expect("lock-session captures the target session identity");
    let killed = handler
        .handle(Request::KillSession(KillSessionRequest {
            target: session.clone(),
            kill_all_except_target: false,
            clear_alerts: false,
            kill_group: false,
        }))
        .await;
    assert!(matches!(killed, Response::KillSession(_)), "{killed:?}");
    create_session(&handler, &session).await;
    let replacement_session_id = handler
        .state
        .lock()
        .await
        .sessions
        .session(&session)
        .expect("replacement session exists")
        .id();
    assert_ne!(replacement_session_id, original_session_id);
    let (replacement_tx, mut replacement_rx) = mpsc::unbounded_channel();
    let replacement_id = handler
        .register_attach(replacement_pid, session, replacement_tx)
        .await;
    pause.release();

    let response = lock.await.expect("lock-session task joins");
    assert!(matches!(response, Response::LockSession(_)), "{response:?}");
    let active_attach = handler.active_attach.lock().await;
    let replacement = active_attach
        .by_pid
        .get(&replacement_pid)
        .expect("replacement session attach survives");
    assert_eq!(replacement.id, replacement_id);
    assert_eq!(replacement.session_id, replacement_session_id);
    assert!(!replacement.suspended);
    while let Ok(control) = replacement_rx.try_recv() {
        assert!(!matches!(control, AttachControl::LockShellCommand(_)));
    }
}