Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coroutine should require 'static stack too #43

Open
zetanumbers opened this issue Dec 5, 2024 · 1 comment · May be fixed by #44
Open

Coroutine should require 'static stack too #43

zetanumbers opened this issue Dec 5, 2024 · 1 comment · May be fixed by #44

Comments

@zetanumbers
Copy link
Contributor

Oops again, didn't notice.

use std::{hint::black_box, mem::forget, sync::mpsc};

use corosensei::{stack::DefaultStack, Coroutine};

type Data = [i32; 10];

struct Input {
    data: Data,
    rx1_and_tx2: Option<(mpsc::Receiver<()>, mpsc::SyncSender<Data>)>,
}

fn main() {
    let (tx1, rx1) = mpsc::sync_channel(1);
    let (tx2, rx2) = mpsc::sync_channel(1);
    let mut stack = DefaultStack::default();
    let initial_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    {
        let mut coroutine = Coroutine::with_stack(&mut stack, coroutine_body);
        coroutine.resume(Input {
            data: initial_data,
            rx1_and_tx2: Some((rx1, tx2)),
        });
        forget(coroutine);
    }
    {
        let mut coroutine = Coroutine::with_stack(&mut stack, coroutine_body);
        // This time we overwrite the data on the stack
        coroutine.resume(Input {
            data: [0; 10],
            rx1_and_tx2: None,
        });
        forget(coroutine);
    }
    tx1.send(()).unwrap();
    let data_race = rx2.recv().unwrap();
    assert_eq!(initial_data, data_race);
}

fn coroutine_body(yielder: &corosensei::Yielder<Input, ()>, input: Input) {
    let Input {
        mut data,
        rx1_and_tx2,
    } = input;
    black_box(&mut data);
    let data = &data;
    let Some((rx1, tx2)) = rx1_and_tx2 else {
        return;
    };
    std::thread::scope(move |scope| {
        let _thrd = scope.spawn(move || {
            rx1.recv().unwrap();
            tx2.send(*data).unwrap();
        });
        yielder.suspend(());
        panic!();
    });
    black_box(&data);
}
@zetanumbers
Copy link
Contributor Author

Hmm, I'm not sure if safety rules for the Stack trait would permit this behavior too? For example imagine creating a reference-counted stack which locks mutex on calls to any method and unlocks it from the drop implementation.

However, if we would have the hypothetical Forget auto trait, there wouldn't be an issue but a limitation of Coroutine being !Forget unless stack type is 'static. Thus I believe placing requirement Stack: 'static in a where clause of the Coroutine::with_stack constructor does make sense.

@zetanumbers zetanumbers linked a pull request Dec 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant