├── .github └── workflows │ ├── rustfmt.yml │ └── tests.yml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples ├── communicating.rs ├── join.rs ├── kill.rs ├── nested.rs ├── simple.rs └── stdout.rs ├── src ├── builder.rs └── lib.rs └── tests ├── nested.rs └── test_harness.rs /.github/workflows/rustfmt.yml: -------------------------------------------------------------------------------- 1 | name: Rustfmt 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Run rustfmt 13 | run: cargo fmt -- --check 14 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v1 12 | - name: Build 13 | run: cargo build --verbose 14 | - name: Run simple example 15 | run: cargo run --example simple 16 | - name: Run communicating example 17 | run: cargo run --example communicating 18 | - name: Run join example 19 | run: cargo run --example join 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mitosis" 3 | version = "0.1.1" 4 | authors = ["Manish Goregaokar "] 5 | edition = "2018" 6 | license = "MIT/Apache-2.0" 7 | repository = "https://github.com/Manishearth/mitosis" 8 | description = "Crate providing the ability to spawn processes with custom closures" 9 | 10 | 11 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 12 | 13 | [dependencies] 14 | ipc-channel = "0.15.0" 15 | serde = { version = "1.0.130", features = ["derive"] } 16 | lazy_static = "1.4.0" 17 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Manish Goregaokar 4 | 5 | Permission is hereby granted, free of charge, to any 6 | person obtaining a copy of this software and associated 7 | documentation files (the "Software"), to deal in the 8 | Software without restriction, including without 9 | limitation the rights to use, copy, modify, merge, 10 | publish, distribute, sublicense, and/or sell copies of 11 | the Software, and to permit persons to whom the Software 12 | is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice 16 | shall be included in all copies or substantial portions 17 | of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 20 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 21 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 22 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 23 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 24 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 26 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | DEALINGS IN THE SOFTWARE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## mitosis 2 | 3 | [![Build Status](https://github.com/manishearth/mitosis/workflows/Tests/badge.svg)](https://github.com/Manishearth/mitosis/actions) 4 | [![Current Version](https://img.shields.io/crates/v/mitosis.svg)](https://crates.io/crates/mitosis) 5 | [![License: MIT/Apache-2.0](https://img.shields.io/crates/l/mitosis.svg)](#license) 6 | 7 | > "AWS Lambda for your local machine" 8 | > 9 | > -- [@jdm](https://github.com/jdm) 10 | 11 | This crate provides `mitosis::spawn()`, which is similar to `thread::spawn()` but will spawn a new process instead. 12 | 13 | 14 | ```rust 15 | 16 | fn main() { 17 | // Needs to be near the beginning of your program 18 | mitosis::init(); 19 | 20 | // some code 21 | let some_data = 5; 22 | mitosis::spawn(some_data, |data| { 23 | println!("hello from another process, your data is {}", data); 24 | }); 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /examples/communicating.rs: -------------------------------------------------------------------------------- 1 | use ipc_channel::ipc; 2 | use mitosis; 3 | 4 | // This example dekonstrates sending IPC channels over to the other process 5 | // 6 | // Waiting on the process' result is better done using JoinHandle as shown in the 7 | // `join` example 8 | fn main() { 9 | mitosis::init(); 10 | let five = fibonacci_par(5); 11 | let ten = fibonacci_par(10); 12 | let thirty = fibonacci_par(30); 13 | assert_eq!(five.recv().unwrap(), 5); 14 | assert_eq!(ten.recv().unwrap(), 55); 15 | assert_eq!(thirty.recv().unwrap(), 832_040); 16 | println!("Successfully calculated fibonacci values!"); 17 | } 18 | 19 | fn fibonacci_par(n: u32) -> ipc::IpcReceiver { 20 | let (tx, rx) = ipc::channel().unwrap(); 21 | 22 | mitosis::spawn((n, tx), |(n, tx)| { 23 | tx.send(fibonacci(n)).unwrap(); 24 | }); 25 | rx 26 | } 27 | 28 | fn fibonacci(n: u32) -> u32 { 29 | if n <= 2 { 30 | return 1; 31 | } 32 | fibonacci(n - 1) + fibonacci(n - 2) 33 | } 34 | -------------------------------------------------------------------------------- /examples/join.rs: -------------------------------------------------------------------------------- 1 | use mitosis; 2 | 3 | fn main() { 4 | mitosis::init(); 5 | let five = mitosis::spawn(5, fibonacci); 6 | let ten = mitosis::spawn(10, fibonacci); 7 | let thirty = mitosis::spawn(30, fibonacci); 8 | assert_eq!(five.join().unwrap(), 5); 9 | assert_eq!(ten.join().unwrap(), 55); 10 | assert_eq!(thirty.join().unwrap(), 832_040); 11 | println!("Successfully calculated fibonacci values!"); 12 | } 13 | 14 | fn fibonacci(n: u32) -> u32 { 15 | if n <= 2 { 16 | return 1; 17 | } 18 | fibonacci(n - 1) + fibonacci(n - 2) 19 | } 20 | -------------------------------------------------------------------------------- /examples/kill.rs: -------------------------------------------------------------------------------- 1 | use mitosis; 2 | 3 | #[allow(clippy::empty_loop)] 4 | fn main() { 5 | mitosis::init(); 6 | 7 | let handle = mitosis::spawn((), |()| loop {}); 8 | 9 | handle.kill().unwrap(); 10 | } 11 | -------------------------------------------------------------------------------- /examples/nested.rs: -------------------------------------------------------------------------------- 1 | use mitosis; 2 | 3 | fn main() { 4 | mitosis::init(); 5 | let five = mitosis::spawn(5, |x| { 6 | println!("1"); 7 | let x = mitosis::spawn(x, |y| { 8 | println!("2"); 9 | y 10 | }) 11 | .join() 12 | .unwrap(); 13 | println!("3"); 14 | x 15 | }) 16 | .join() 17 | .unwrap(); 18 | println!("4"); 19 | assert_eq!(five, 5); 20 | } 21 | -------------------------------------------------------------------------------- /examples/simple.rs: -------------------------------------------------------------------------------- 1 | use mitosis; 2 | 3 | use std::thread::sleep; 4 | use std::time::Duration; 5 | 6 | fn main() { 7 | mitosis::init(); 8 | 9 | mitosis::spawn((1, 2), |(a, b)| { 10 | println!("{:?} {:?}", a, b); 11 | }); 12 | 13 | sleep(Duration::from_secs(2)); 14 | } 15 | -------------------------------------------------------------------------------- /examples/stdout.rs: -------------------------------------------------------------------------------- 1 | use mitosis; 2 | 3 | use std::io::Read; 4 | 5 | fn main() { 6 | mitosis::init(); 7 | 8 | let mut builder = mitosis::Builder::new(); 9 | builder.stdout(std::process::Stdio::piped()); 10 | let mut handle = builder.spawn((1, 2), |(a, b)| { 11 | println!("{:?} {:?}", a, b); 12 | }); 13 | 14 | let mut s = String::new(); 15 | handle 16 | .stdout() 17 | .take() 18 | .unwrap() 19 | .read_to_string(&mut s) 20 | .unwrap(); 21 | assert_eq!(s, "1 2\n"); 22 | } 23 | -------------------------------------------------------------------------------- /src/builder.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::ffi::OsStr; 3 | use std::ffi::OsString; 4 | use std::process::Stdio; 5 | 6 | #[derive(Debug, Default)] 7 | pub struct Builder { 8 | pub(crate) stdin: Option, 9 | pub(crate) stdout: Option, 10 | pub(crate) stderr: Option, 11 | pub(crate) envs: HashMap, 12 | } 13 | 14 | impl Builder { 15 | pub fn new() -> Self { 16 | Self { 17 | stdin: None, 18 | stdout: None, 19 | stderr: None, 20 | envs: std::env::vars_os().collect(), 21 | } 22 | } 23 | 24 | /// Set an environment variable in the spawned process. Equivalent to `Command::env` 25 | pub fn env(&mut self, key: K, val: V) -> &mut Self 26 | where 27 | K: AsRef, 28 | V: AsRef, 29 | { 30 | self.envs 31 | .insert(key.as_ref().to_owned(), val.as_ref().to_owned()); 32 | self 33 | } 34 | 35 | /// Set environment variables in the spawned process. Equivalent to `Command::envs` 36 | pub fn envs(&mut self, vars: I) -> &mut Self 37 | where 38 | I: IntoIterator, 39 | K: AsRef, 40 | V: AsRef, 41 | { 42 | self.envs.extend( 43 | vars.into_iter() 44 | .map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned())), 45 | ); 46 | self 47 | } 48 | 49 | /// Removes an environment variable in the spawned process. Equivalent to `Command::env_remove` 50 | pub fn env_remove>(&mut self, key: K) -> &mut Self { 51 | self.envs.remove(key.as_ref()); 52 | self 53 | } 54 | 55 | /// Clears all environment variables in the spawned process. Equivalent to `Command::env_clear` 56 | pub fn env_clear(&mut self) -> &mut Self { 57 | self.envs.clear(); 58 | self 59 | } 60 | 61 | /// Captures the `stdin` of the spawned process, allowing you to manually send data via `JoinHandle::stdin` 62 | pub fn stdin>(&mut self, cfg: T) -> &mut Self { 63 | self.stdin = Some(cfg.into()); 64 | self 65 | } 66 | 67 | /// Captures the `stdout` of the spawned process, allowing you to manually receive data via `JoinHandle::stdout` 68 | pub fn stdout>(&mut self, cfg: T) -> &mut Self { 69 | self.stdout = Some(cfg.into()); 70 | self 71 | } 72 | 73 | /// Captures the `stderr` of the spawned process, allowing you to manually receive data via `JoinHandle::stderr` 74 | pub fn stderr>(&mut self, cfg: T) -> &mut Self { 75 | self.stderr = Some(cfg.into()); 76 | self 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! This crate provides the ability to spawn processes with a function similar 2 | //! to `thread::spawn` 3 | //! 4 | //! To use this crate, call `mitosis::init()` at the beginning of your `main()`, 5 | //! and then anywhere in your program you may call `mitosis::spawn()`: 6 | //! 7 | //! ```rust,no_run 8 | //! let data = vec![1, 2, 3, 4]; 9 | //! mitosis::spawn(data, |data| { 10 | //! // This will run in a separate process 11 | //! println!("Received data {:?}", data); 12 | //! }); 13 | //!``` 14 | //! 15 | //! `mitosis::spawn()` can pass arbitrary serializable data, including IPC senders 16 | //! and receivers from the `ipc-channel` crate, down to the new process. 17 | 18 | use ipc_channel::ipc::{ 19 | self, IpcError, IpcOneShotServer, IpcReceiver, IpcSender, OpaqueIpcReceiver, OpaqueIpcSender, 20 | }; 21 | use serde::{Deserialize, Serialize}; 22 | use std::ffi::OsStr; 23 | use std::path::PathBuf; 24 | use std::process::{ChildStderr, ChildStdin, ChildStdout}; 25 | use std::sync::atomic::{AtomicBool, Ordering}; 26 | use std::{env, mem, process}; 27 | 28 | mod builder; 29 | 30 | pub use builder::*; 31 | 32 | const ENV_NAME: &str = "MITOSIS_CONTENT_PROCESS_ID"; 33 | 34 | /// Initialize mitosis 35 | /// 36 | /// This MUST be called near the top of your main(), before 37 | /// you do any environment variable processing. Any code found before this will also 38 | /// be executed for each spawned child process. 39 | /// 40 | /// # Safety 41 | /// It is not unsafe to omit this function, however `mitosis::spawn` 42 | /// may lead to unexpected behavior. 43 | pub fn init() { 44 | if let Ok(token) = env::var(ENV_NAME) { 45 | // Clear environment variable so processes spawned from the `spawn` closure can 46 | // themselves be using `mitosis` 47 | std::env::remove_var(ENV_NAME); 48 | bootstrap_ipc(token); 49 | } 50 | } 51 | 52 | static IN_TEST_ENV: AtomicBool = AtomicBool::new(false); 53 | 54 | /// Initialize `mitosis` within a `#[test]`. You need to also have 55 | /// ```rust 56 | /// #[test] 57 | /// fn mitosis() { 58 | /// init_test() 59 | /// } 60 | /// ``` 61 | /// in your crate root, because `spawn` calls the test binary 62 | /// with `--exact mitosis` 63 | /// 64 | /// Note that using `mitosis` within tests is slow. Whenever you call `spawn` 65 | /// the entire test harness is executed before actually running your closure. 66 | /// 67 | /// Note that if you use `init` instead of `init_test` inside the `mitosis` test, 68 | /// then you can't do nested calls to `mitosis::spawn` inside tests 69 | /// 70 | /// # Safety 71 | /// It is not unsafe to omit this function, 72 | /// however `mitosis::spawn` may lead to unexpected behavior. 73 | pub fn init_test() { 74 | // Set the variable before running `init` so that nested `spawn` calls work 75 | IN_TEST_ENV.store(true, Ordering::Relaxed); 76 | init(); 77 | } 78 | 79 | #[derive(Serialize, Deserialize, Debug)] 80 | struct BootstrapData { 81 | wrapper_offset: isize, 82 | args_receiver: OpaqueIpcReceiver, 83 | return_sender: OpaqueIpcSender, 84 | } 85 | 86 | fn bootstrap_ipc(token: String) { 87 | let connection_bootstrap: IpcSender> = 88 | IpcSender::connect(token).unwrap(); 89 | let (tx, rx) = ipc::channel().unwrap(); 90 | connection_bootstrap.send(tx).unwrap(); 91 | let bootstrap_data = rx.recv().unwrap(); 92 | unsafe { 93 | let ptr = bootstrap_data.wrapper_offset + init as *const () as isize; 94 | let func: fn(OpaqueIpcReceiver, OpaqueIpcSender) = mem::transmute(ptr); 95 | func(bootstrap_data.args_receiver, bootstrap_data.return_sender); 96 | } 97 | process::exit(0); 98 | } 99 | 100 | /// Spawn a new process to run a function with some payload 101 | /// 102 | /// ```rust,no_run 103 | /// let data = vec![1, 2, 3, 4]; 104 | /// mitosis::spawn(data, |data| { 105 | /// // This will run in a separate process 106 | /// println!("Received data {:?}", data); 107 | /// }); 108 | /// ``` 109 | /// 110 | /// The function itself cannot capture anything from its environment, but you can 111 | /// explicitly pass down data through the `args` parameter. This function will panic if 112 | /// you pass a closure that captures anything from its environment. 113 | /// 114 | /// The `JoinHandle` returned by this function can be used to wait for 115 | /// the child process to finish, and obtain the return value of the function it executed. 116 | /// 117 | /// ```rust,no_run 118 | /// let data = vec![1, 1, 2, 3, 3, 5, 4, 1]; 119 | /// let handle = mitosis::spawn(data, |mut data| { 120 | /// // This will run in a separate process 121 | /// println!("Received data {:?}", data); 122 | /// data.dedup(); 123 | /// }); 124 | /// // do some other work 125 | /// println!("Deduplicated {:?}", handle.join()); 126 | /// ``` 127 | pub fn spawn< 128 | F: FnOnce(A) -> R + Copy, 129 | A: Serialize + for<'de> Deserialize<'de>, 130 | R: Serialize + for<'de> Deserialize<'de>, 131 | >( 132 | args: A, 133 | f: F, 134 | ) -> JoinHandle { 135 | Builder::new().spawn(args, f) 136 | } 137 | 138 | trait ZstAssert: Sized { 139 | const MITOSIS_CLOSURE_CANNOT_BORROW_DATA: () = [()][(mem::size_of::() != 0) as usize]; 140 | } 141 | 142 | impl ZstAssert for T {} 143 | 144 | impl Builder { 145 | pub fn spawn< 146 | F: FnOnce(A) -> R + Copy, 147 | A: Serialize + for<'de> Deserialize<'de>, 148 | R: Serialize + for<'de> Deserialize<'de>, 149 | >( 150 | self, 151 | args: A, 152 | _: F, 153 | ) -> JoinHandle { 154 | #[allow(path_statements)] 155 | { 156 | F::MITOSIS_CLOSURE_CANNOT_BORROW_DATA; 157 | } 158 | 159 | let (server, token) = IpcOneShotServer::>::new().unwrap(); 160 | let me = if cfg!(target_os = "linux") { 161 | // will work even if exe is moved 162 | let path: PathBuf = "/proc/self/exe".into(); 163 | if path.is_file() { 164 | path 165 | } else { 166 | // might not exist, e.g. on chroot 167 | env::current_exe().unwrap() 168 | } 169 | } else { 170 | env::current_exe().unwrap() 171 | }; 172 | let mut child = process::Command::new(me); 173 | assert!( 174 | !self.envs.contains_key(OsStr::new(ENV_NAME)), 175 | "cannot spawn mitosis process with `{}` still set", 176 | ENV_NAME 177 | ); 178 | child.envs(self.envs.into_iter()); 179 | child.env(ENV_NAME, token); 180 | if IN_TEST_ENV.load(Ordering::Relaxed) { 181 | // we expect the user to have supplied a `#[test] fn mitosis() { mitosis::init_test() } 182 | child.arg("mitosis"); 183 | // makes sure we don't run any other tests 184 | child.arg("--exact"); 185 | // reduces boilerplate CPU time 186 | child.arg("--test-threads=1"); 187 | // reduces stderr noise 188 | child.arg("-q"); 189 | } 190 | if let Some(stdin) = self.stdin { 191 | child.stdin(stdin); 192 | } 193 | if let Some(stdout) = self.stdout { 194 | child.stdout(stdout); 195 | } 196 | if let Some(stderr) = self.stderr { 197 | child.stderr(stderr); 198 | } 199 | let process = child.spawn().unwrap(); 200 | 201 | let (_rx, tx) = server.accept().unwrap(); 202 | 203 | let (args_tx, args_rx) = ipc::channel().unwrap(); 204 | let (return_tx, return_rx) = ipc::channel().unwrap(); 205 | args_tx.send(args).unwrap(); 206 | // ASLR mitigation 207 | let init_loc = init as *const () as isize; 208 | let wrapper_offset = run_func:: as *const () as isize - init_loc; 209 | let bootstrap = BootstrapData { 210 | wrapper_offset, 211 | args_receiver: args_rx.to_opaque(), 212 | return_sender: return_tx.to_opaque(), 213 | }; 214 | tx.send(bootstrap).unwrap(); 215 | JoinHandle { 216 | recv: return_rx, 217 | process, 218 | } 219 | } 220 | } 221 | 222 | unsafe fn run_func< 223 | F: FnOnce(A) -> R, 224 | A: Serialize + for<'de> Deserialize<'de>, 225 | R: Serialize + for<'de> Deserialize<'de>, 226 | >( 227 | recv: OpaqueIpcReceiver, 228 | sender: OpaqueIpcSender, 229 | ) { 230 | let function: F = mem::zeroed(); 231 | 232 | let args = recv.to().recv().unwrap(); 233 | let ret = function(args); 234 | let _ = sender.to().send(ret); 235 | } 236 | 237 | /// This value is returned by `mitosis::spawn` and lets you 238 | /// wait on the result of the child process' computation 239 | pub struct JoinHandle { 240 | recv: IpcReceiver, 241 | process: process::Child, 242 | } 243 | 244 | impl Deserialize<'de>> JoinHandle { 245 | /// Wait for the child process to return a result 246 | pub fn join(self) -> Result { 247 | self.recv.recv() 248 | } 249 | 250 | /// Kill the child process. 251 | pub fn kill(mut self) -> std::io::Result<()> { 252 | self.process.kill() 253 | } 254 | 255 | /// Fetch the `stdin` handle if it has been captured 256 | pub fn stdin(&mut self) -> &mut Option { 257 | &mut self.process.stdin 258 | } 259 | 260 | /// Fetch the `stdout` handle if it has been captured 261 | pub fn stdout(&mut self) -> &mut Option { 262 | &mut self.process.stdout 263 | } 264 | 265 | /// Fetch the `stderr` handle if it has been captured 266 | pub fn stderr(&mut self) -> &mut Option { 267 | &mut self.process.stderr 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /tests/nested.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | fn mitosis() { 3 | mitosis::init_test(); 4 | } 5 | 6 | #[test] 7 | fn nested() { 8 | mitosis::init_test(); 9 | let five = mitosis::spawn(5, |x| { 10 | println!("1"); 11 | let x = mitosis::spawn(x, |y| { 12 | println!("2"); 13 | y 14 | }) 15 | .join() 16 | .unwrap(); 17 | println!("3"); 18 | x 19 | }) 20 | .join() 21 | .unwrap(); 22 | println!("4"); 23 | assert_eq!(five, 5); 24 | } 25 | -------------------------------------------------------------------------------- /tests/test_harness.rs: -------------------------------------------------------------------------------- 1 | use mitosis::{init_test, spawn}; 2 | 3 | #[test] 4 | fn normal_test() {} 5 | 6 | #[test] 7 | fn mitosis() { 8 | init_test(); 9 | } 10 | 11 | #[test] 12 | fn using_init_test_but_not_spawn() { 13 | init_test(); 14 | } 15 | 16 | #[test] 17 | fn using_init_test_and_spawn() { 18 | init_test(); 19 | let val = spawn(42, |x| x / 2).join().unwrap(); 20 | assert_eq!(val, 21); 21 | } 22 | --------------------------------------------------------------------------------