fix(resource): share one stdin cursor across handles
This commit is contained in:
1 parent
3b13d8499a
commit
64a6b48866
2 files changed
+44
-17
No files matched your search
@@ -203,8 +203,6 @@ pub struct StdHost {
|
|||||||
broker_audit: Arc<Mutex<Vec<CapabilityAudit>>>,
|
broker_audit: Arc<Mutex<Vec<CapabilityAudit>>>,
|
||||||
/// Host-issued opaque plugin handles.
|
/// Host-issued opaque plugin handles.
|
||||||
pub handles: PluginHandleTable,
|
pub handles: PluginHandleTable,
|
||||||
/// Run-scoped stdin bytes supplied by the composition root.
|
|
||||||
stdin: Vec<u8>,
|
|
||||||
/// Program stderr captured separately from stdout and diagnostics.
|
/// Program stderr captured separately from stdout and diagnostics.
|
||||||
stderr: Vec<u8>,
|
stderr: Vec<u8>,
|
||||||
/// Host-owned resource/stream table (ADR-0025).
|
/// Host-owned resource/stream table (ADR-0025).
|
||||||
@@ -271,7 +269,6 @@ impl StdHost {
|
|||||||
vendor_capabilities: BTreeSet::new(),
|
vendor_capabilities: BTreeSet::new(),
|
||||||
broker_audit: Arc::new(Mutex::new(Vec::new())),
|
broker_audit: Arc::new(Mutex::new(Vec::new())),
|
||||||
handles: PluginHandleTable::default(),
|
handles: PluginHandleTable::default(),
|
||||||
stdin: Vec::new(),
|
|
||||||
stderr: Vec::new(),
|
stderr: Vec::new(),
|
||||||
resources: ResourceTable::new(),
|
resources: ResourceTable::new(),
|
||||||
resource_providers: BTreeMap::new(),
|
resource_providers: BTreeMap::new(),
|
||||||
@@ -487,7 +484,7 @@ impl StdHost {
|
|||||||
/// This does not start a thread, spawn a process or read a hidden second
|
/// This does not start a thread, spawn a process or read a hidden second
|
||||||
/// channel. A program reads these bytes only through `stdio/stdin`.
|
/// channel. A program reads these bytes only through `stdio/stdin`.
|
||||||
pub fn with_stdin(mut self, bytes: impl Into<Vec<u8>>) -> Self {
|
pub fn with_stdin(mut self, bytes: impl Into<Vec<u8>>) -> Self {
|
||||||
self.stdin = bytes.into();
|
self.resources.set_stdin(bytes.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -795,7 +792,7 @@ impl StdHost {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
let handle = match descriptor {
|
let handle = match descriptor {
|
||||||
"stdin" => self.resources.open_stdin(self.stdin.clone()),
|
"stdin" => self.resources.open_stdin(),
|
||||||
"stderr" => self.resources.open_stderr(),
|
"stderr" => self.resources.open_stderr(),
|
||||||
"stdout" => self.resources.open_stdout(),
|
"stdout" => self.resources.open_stdout(),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
|||||||
@@ -76,10 +76,14 @@ pub struct ResourceRead {
|
|||||||
pub eof: bool,
|
pub eof: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct StdinCursor {
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
position: usize,
|
||||||
|
}
|
||||||
|
|
||||||
enum Stream {
|
enum Stream {
|
||||||
Stdin {
|
Stdin {
|
||||||
bytes: Vec<u8>,
|
cursor: Arc<Mutex<StdinCursor>>,
|
||||||
position: usize,
|
|
||||||
closed: bool,
|
closed: bool,
|
||||||
},
|
},
|
||||||
Stdout {
|
Stdout {
|
||||||
@@ -135,6 +139,7 @@ pub struct ResourceTable {
|
|||||||
run_id: u64,
|
run_id: u64,
|
||||||
next_resource: u64,
|
next_resource: u64,
|
||||||
slots: BTreeMap<u64, Slot>,
|
slots: BTreeMap<u64, Slot>,
|
||||||
|
stdin: Arc<Mutex<StdinCursor>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ResourceTable {
|
impl Default for ResourceTable {
|
||||||
@@ -150,6 +155,10 @@ impl ResourceTable {
|
|||||||
run_id: NEXT_RUN_ID.fetch_add(1, Ordering::Relaxed),
|
run_id: NEXT_RUN_ID.fetch_add(1, Ordering::Relaxed),
|
||||||
next_resource: 1,
|
next_resource: 1,
|
||||||
slots: BTreeMap::new(),
|
slots: BTreeMap::new(),
|
||||||
|
stdin: Arc::new(Mutex::new(StdinCursor {
|
||||||
|
bytes: Vec::new(),
|
||||||
|
position: 0,
|
||||||
|
})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,14 +177,18 @@ impl ResourceTable {
|
|||||||
self.insert(Stream::Stderr { closed: false })
|
self.insert(Stream::Stderr { closed: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register a run-scoped stdin stream over already supplied bytes.
|
/// Set the run-scoped stdin bytes at assembly time.
|
||||||
///
|
///
|
||||||
/// The host supplies bytes at assembly time; `read` chunks them with EOF.
|
/// All `stdio/stdin` handles share this cursor so repeated console reads
|
||||||
/// There is no blocking OS read and no hidden second input channel here.
|
/// continue through the input instead of restarting at byte zero.
|
||||||
pub fn open_stdin(&mut self, bytes: Vec<u8>) -> ResourceHandle {
|
pub fn set_stdin(&mut self, bytes: Vec<u8>) {
|
||||||
|
self.stdin = Arc::new(Mutex::new(StdinCursor { bytes, position: 0 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register a read-only handle over the run-scoped stdin cursor.
|
||||||
|
pub fn open_stdin(&mut self) -> ResourceHandle {
|
||||||
self.insert(Stream::Stdin {
|
self.insert(Stream::Stdin {
|
||||||
bytes,
|
cursor: Arc::clone(&self.stdin),
|
||||||
position: 0,
|
|
||||||
closed: false,
|
closed: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -312,10 +325,18 @@ impl ResourceTable {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
match &mut slot.stream {
|
match &mut slot.stream {
|
||||||
Stream::Stdin {
|
Stream::Stdin { cursor, .. } => {
|
||||||
bytes, position, ..
|
let mut cursor = cursor.lock().map_err(|_| stdin_lock_error(span))?;
|
||||||
|
let start = cursor.position;
|
||||||
|
let end = start.saturating_add(max_bytes).min(cursor.bytes.len());
|
||||||
|
let chunk = cursor.bytes[start..end].to_vec();
|
||||||
|
cursor.position = end;
|
||||||
|
Ok(ResourceRead {
|
||||||
|
bytes: chunk,
|
||||||
|
eof: end >= cursor.bytes.len(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
| Stream::File {
|
Stream::File {
|
||||||
bytes, position, ..
|
bytes, position, ..
|
||||||
} => {
|
} => {
|
||||||
let start = *position;
|
let start = *position;
|
||||||
@@ -524,6 +545,14 @@ fn provider_lock_error(span: Option<Span>) -> RuntimeError {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn stdin_lock_error(span: Option<Span>) -> RuntimeError {
|
||||||
|
RuntimeError::new(
|
||||||
|
ErrorCategory::HostError,
|
||||||
|
span,
|
||||||
|
"stdin cursor lock is poisoned",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn unknown_handle(span: Option<Span>) -> RuntimeError {
|
fn unknown_handle(span: Option<Span>) -> RuntimeError {
|
||||||
RuntimeError::new(
|
RuntimeError::new(
|
||||||
ErrorCategory::ResourceFault,
|
ErrorCategory::ResourceFault,
|
||||||
@@ -601,7 +630,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn stdin_reads_are_chunked_and_end_at_eof() {
|
fn stdin_reads_are_chunked_and_end_at_eof() {
|
||||||
let mut table = ResourceTable::new();
|
let mut table = ResourceTable::new();
|
||||||
let stdin = table.open_stdin(b"ab".to_vec());
|
table.set_stdin(b"ab".to_vec());
|
||||||
|
let stdin = table.open_stdin();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
table.read(stdin, 1, None).unwrap(),
|
table.read(stdin, 1, None).unwrap(),
|
||||||
ResourceRead {
|
ResourceRead {
|
||||||
|
|||||||
Reference in new issue
Block a user