Skip to content

Instantly share code, notes, and snippets.

@rtyler
Last active May 25, 2020
Embed
What would you like to do?
trying to reproduce a BufReader error in async_std.

Reproducing errors in async-std/async-tls

The binary and client.rb can both be run in plaintext mode (no arguments) and everything will work out fine. When both are run with the tls argument, the error can be reproduced, e.g ruby client.rb tls and ./target/debug/big-async-reader tls

---
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `32768`,
 right: `0`', <::std::macros::panic macros>:5:6
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1063
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1426
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:204
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:224
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:470
  11: rust_begin_unwind
             at src/libstd/panicking.rs:378
  12: std::panicking::begin_panic_fmt
             at src/libstd/panicking.rs:332
  13: async_std::io::buf_read::lines::read_line_internal
             at ./<::std::macros::panic macros>:5
  14: <async_std::io::buf_read::lines::Lines<R> as futures_core::stream::Stream>::poll_next
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/io/buf_read/lines.rs:37
  15: <async_std::stream::stream::next::NextFuture<T> as core::future::future::Future>::poll
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/stream/stream/next.rs:17
  16: std::future::poll_with_tls_context
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/future.rs:102
  17: big_async_reader::tls_server::{{closure}}
             at src/main.rs:25
  18: <std::future::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/future.rs:44
  19: std::future::poll_with_tls_context
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/future.rs:102
  20: async_std::task::block_on::block_on::{{closure}}
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/block_on.rs:68
  21: <std::future::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/future.rs:44
  22: async_std::task::block_on::run::{{closure}}
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/block_on.rs:128
  23: std::thread::local::LocalKey<T>::try_with
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/thread/local.rs:262
  24: std::thread::local::LocalKey<T>::with
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/thread/local.rs:239
  25: async_std::task::block_on::run
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/block_on.rs:118
  26: async_std::task::block_on::block_on::{{closure}}
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/block_on.rs:72
  27: async_std::task::task::Task::set_current::{{closure}}
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/task.rs:129
  28: std::thread::local::LocalKey<T>::try_with
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/thread/local.rs:262
  29: std::thread::local::LocalKey<T>::with
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/thread/local.rs:239
  30: async_std::task::task::Task::set_current
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/task.rs:124
  31: async_std::task::block_on::block_on
             at /home/tyler/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.5.0/src/task/block_on.rs:72
  32: big_async_reader::main
             at src/main.rs:54
  33: std::rt::lang_start::{{closure}}
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/rt.rs:67
  34: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  35: std::panicking::try::do_call
             at src/libstd/panicking.rs:303
  36: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:86
  37: std::panicking::try
             at src/libstd/panicking.rs:281
  38: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  39: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  40: std::rt::lang_start
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/rt.rs:67
  41: main
  42: __libc_start_main
  43: <unknown>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---
[package]
name = "big-async-reader"
version = "0.1.0"
authors = ["R. Tyler Croy <rtyler@brokenco.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-std = "~1.5.0"
futures = { version = "~0.3.4", features = ["thread-pool"] }
async-tls = "~0.7.0"
rustls = "~0.17.0"
#!/usr/bin/env ruby
require 'json'
require 'socket'
require 'openssl'
use_tls = ARGV.first == 'tls'
sock = TCPSocket.new('localhost', 4321)
if use_tls
tls_client = OpenSSL::SSL::SSLSocket.new(sock)
tls_client.connect
stream = tls_client
else
stream = sock
end
stream.write "#{'0' * (2 ** 13)} 8k\n"
stream.write "#{'0' * (2 ** 14)} 16k\n"
stream.write "#{'0' * (2 ** 15)} 32k\n"
stream.write "#{'0' * (2 ** 16)} 64k\n"
stream.write "#{'0' * (2 ** 17)} 128k\n"
tls_client.sysclose if use_tls
sock.close
/**
* Hopefully a reproduction case for debug_assert trigger when reading lines from
* a TLS socket with async_std
*/
use async_std::{io::BufReader, net::*, prelude::*, sync::Arc, task};
use async_tls::TlsAcceptor;
use rustls::internal::pemfile::{certs, rsa_private_keys};
use rustls::{
NoClientAuth, ServerConfig,
};
async fn tls_server() -> Result<(), std::io::Error> {
let config = load_tls_config();
let acceptor = TlsAcceptor::from(Arc::new(config));
let listener = TcpListener::bind("127.0.0.1:4321").await?;
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let stream = stream?;
let handshake = acceptor.accept(stream).await?;
let reader = BufReader::new(handshake);
let mut lines = reader.lines();
while let Some(line) = lines.next().await {
let line = line?;
println!("L: {}", line);
}
}
Ok(())
}
async fn server() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:4321").await?;
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let stream = stream?;
let reader = BufReader::new(stream);
let mut lines = reader.lines();
while let Some(line) = lines.next().await {
let line = line?;
println!("L: {}", line);
}
}
Ok(())
}
fn main() -> Result<(), std::io::Error> {
println!("Send me your lines on localhost:4321");
if let Some(proto) = std::env::args().nth(1) {
if proto == "tls" {
println!("Using TLS");
return task::block_on(tls_server());
}
}
else {
println!("Using plaintext");
return task::block_on(server());
}
Ok(())
}
fn load_tls_config() -> ServerConfig {
let certs = certs(&mut std::io::BufReader::new(std::fs::File::open("./cert.pem").unwrap())).unwrap();
let mut keys = rsa_private_keys(&mut std::io::BufReader::new(std::fs::File::open("./cert-key.pem").unwrap())).unwrap();
let mut config = ServerConfig::new(NoClientAuth::new());
config.set_single_cert(certs, keys.remove(0));
config
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment