Skip to content

Commit cc95b3b

Browse files
authored
Merge pull request #1 from badeend/close-notify
Update signature of `close-notify`
2 parents 6f610ce + 7e57814 commit cc95b3b

File tree

4 files changed

+18
-37
lines changed

4 files changed

+18
-37
lines changed

crates/test-programs/src/bin/tls_sample_application.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test_tls_sample_application() {
3434

3535
tls_output.blocking_write_util(request.as_bytes()).unwrap();
3636
client_connection
37-
.blocking_close_notify(&tls_output)
37+
.blocking_close_output(&tls_output)
3838
.unwrap();
3939
socket.shutdown(ShutdownType::Send).unwrap();
4040
let response = tls_input.blocking_read_to_end().unwrap();

crates/test-programs/src/tls.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::wasi::clocks::monotonic_clock;
2+
use crate::wasi::io::streams::StreamError;
23
use crate::wasi::tls::types::{ClientConnection, ClientHandshake, InputStream, OutputStream};
34

45
const TIMEOUT_NS: u64 = 1_000_000_000;
@@ -23,17 +24,21 @@ impl ClientHandshake {
2324
}
2425

2526
impl ClientConnection {
26-
pub fn blocking_close_notify(
27+
pub fn blocking_close_output(
2728
&self,
2829
output: &OutputStream,
2930
) -> Result<(), crate::wasi::io::error::Error> {
3031
let timeout = monotonic_clock::subscribe_duration(TIMEOUT_NS);
3132
let pollable = output.subscribe();
3233

34+
self.close_output();
35+
3336
loop {
34-
match self.close_notify() {
35-
None => pollable.block_until(&timeout).expect("timed out"),
36-
Some(result) => return result,
37+
match output.check_write() {
38+
Ok(0) => pollable.block_until(&timeout).expect("timed out"),
39+
Ok(_) => unreachable!("After calling close_output, the output stream should never accept new writes again."),
40+
Err(StreamError::Closed) => return Ok(()),
41+
Err(StreamError::LastOperationFailed(e)) => return Err(e),
3742
}
3843
}
3944
}

crates/wasi-tls/src/lib.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ use wasmtime_wasi::OutputStream;
9090
use wasmtime_wasi::{
9191
async_trait,
9292
bindings::io::{
93-
error::Error as HostError,
9493
poll::Pollable as HostPollable,
9594
streams::{InputStream as BoxInputStream, OutputStream as BoxOutputStream},
9695
},
@@ -297,17 +296,8 @@ pub struct ClientConnection {
297296
}
298297

299298
impl<'a> generated::types::HostClientConnection for WasiTlsCtx<'a> {
300-
fn close_notify(
301-
&mut self,
302-
this: Resource<ClientConnection>,
303-
) -> wasmtime::Result<Option<Result<(), wasmtime::component::Resource<HostError>>>> {
304-
let result = self.table.get_mut(&this)?.writer.close_notify();
305-
306-
Ok(match result {
307-
None => None,
308-
Some(Ok(())) => Some(Ok(())),
309-
Some(Err(e)) => Some(Err(self.table.push(anyhow::Error::new(e))?)),
310-
})
299+
fn close_output(&mut self, this: Resource<ClientConnection>) -> wasmtime::Result<()> {
300+
self.table.get_mut(&this)?.writer.close()
311301
}
312302

313303
fn drop(&mut self, this: Resource<ClientConnection>) -> wasmtime::Result<()> {
@@ -524,14 +514,13 @@ impl TlsWriter {
524514
}
525515
}
526516

527-
fn close_notify(&mut self) -> Option<Result<(), io::Error>> {
517+
fn close(&mut self) {
528518
match std::mem::replace(&mut self.state, WriteState::Closed) {
529519
// No write in progress, immediately shut down:
530520
WriteState::Ready(mut stream) => {
531521
self.state = WriteState::Closing(wasmtime_wasi::runtime::spawn(async move {
532522
stream.shutdown().await
533523
}));
534-
None
535524
}
536525

537526
// Schedule the shutdown after the current write has finished:
@@ -540,15 +529,12 @@ impl TlsWriter {
540529
let mut stream = write.await?;
541530
stream.shutdown().await
542531
}));
543-
None
544532
}
545533

546534
WriteState::Closing(t) => {
547535
self.state = WriteState::Closing(t);
548-
None
549536
}
550-
WriteState::Closed => Some(Ok(())),
551-
WriteState::Error(e) => Some(Err(e.into())),
537+
WriteState::Closed | WriteState::Error(_) => {}
552538
}
553539
}
554540

@@ -587,17 +573,9 @@ impl AsyncTlsWriteStream {
587573
AsyncTlsWriteStream(Arc::new(Mutex::new(writer)))
588574
}
589575

590-
fn close_notify(&mut self) -> Option<Result<(), StreamError>> {
591-
let mut writer = match try_lock_for_stream(&self.0) {
592-
Ok(writer) => writer,
593-
Err(err) => return Some(Err(err)),
594-
};
595-
596-
match writer.close_notify() {
597-
None => None,
598-
Some(Ok(())) => Some(Ok(())),
599-
Some(Err(e)) => Some(Err(StreamError::LastOperationFailed(e.into()))),
600-
}
576+
fn close(&mut self) -> wasmtime::Result<()> {
577+
try_lock_for_stream(&self.0)?.close();
578+
Ok(())
601579
}
602580
}
603581

crates/wasi-tls/wit/world.wit

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ world imports {
88

99
@unstable(feature = tls)
1010
interface types {
11-
@unstable(feature = tls)
12-
use wasi:io/error@0.2.3.{error as io-error};
1311
@unstable(feature = tls)
1412
use wasi:io/streams@0.2.3.{input-stream, output-stream};
1513
@unstable(feature = tls)
@@ -27,7 +25,7 @@ interface types {
2725
@unstable(feature = tls)
2826
resource client-connection {
2927
@unstable(feature = tls)
30-
close-notify: func() -> option<result<_, io-error>>;
28+
close-output: func();
3129
}
3230

3331
@unstable(feature = tls)

0 commit comments

Comments
 (0)