Commit Graph
1914 Commits
Author SHA1 Message Date
Mikail Bagishov db0d6d75b3 chore: fix clippy errors (#2571) 2020-05-30 14:06:03 -07:00
xliiv f2f30d4cf6 docs: replace method links with intra-links (#2540) 2020-05-30 20:18:01 +02:00
Geoffry Song c624cb8ce3 io: update AsyncBufRead documentation (#2564) 2020-05-29 14:00:13 +02:00
Mathspy f7574d9023 net: add note about into_split's drop (#2567)
This took me a bit to catch on to because I didn't really think there was any reason to investigate the individual documentation of each half. As someone dealing with TCP streams directly for first time (without previous experience from other languages) this caught me by surprise
2020-05-28 10:11:55 +02:00
Alice Ryhl 954f2b7304 io: fix panic in read_line (#2541)
Fixes: #2532
2020-05-24 23:26:33 +02:00
Geoff Shannon d562e58871 ci: start migrating CI to Github Actions (#2531)
This migrates test_tokio, test_sub_crates, and test_integration to
GitHub Actions, as the first step in the migration from Azure Pipelines.
2020-05-25 01:46:48 +09:00
Jon Gjengset 9f63911adc coop: Undo budget decrement on Pending (#2549)
This patch updates the coop logic so that the budget is only decremented
if a future makes progress (that is, if it returns `Ready`). This is
realized by restoring the budget to its former value after
`poll_proceed` _unless_ the caller indicates that it made progress.

The thinking here is that we always want tasks to make progress when we
poll them. With the way things were, if a task polled 128 resources that
could make no progress, and just returned `Pending`, then a 129th
resource that _could_ make progress would not be polled. Worse yet, this
could manifest as a deadlock, if the first 128 resources were all
_waiting_ for the 129th resource, since it would _never_ be polled.

The downside of this change is that `Pending` resources now do not take
up any part of the budget, even though they _do_ take up time on the
executor. If a task is particularly aggressive (or unoptimized), and
polls a large number of resources that cannot make progress whenever it
is polled, then coop will allow it to run potentially much longer before
yielding than it could before. The impact of this should be relatively
contained though, because tasks that behaved in this way in the past
probably ignored `Pending` _anyway_, so whether a resource returned
`Pending` due to coop or due to lack of progress may not make a
difference to it.
2020-05-21 17:07:23 -04:00
Mikail Bagishov 1e54a35325 io: remove zeroing for AsyncRead implementors (#2525) 2020-05-21 19:42:28 +02:00
Charles Hovine 4f4f4807c3 fs: implement OpenOptionsExt for OpenOptions (#2515)
Trait OpenOptionsExt is now implemented for fs::OpenOption.

In order to access the underlying std::fs::OpenOptions wrapped in
tokio's OpenOption, an as_inner_mut method was added to OpenOption,
only visible to the parent module.

Fixes: #2366
2020-05-21 17:18:58 +02:00
Dmitri Shkurski 8fda5f1984 fs: add DirBuilder (#2524)
The initial idea was to implement  a thin wrapper  around an internally
held `std::fs::DirBuilder` instance.  This, however, didn't work due to
`std::fs::DirBuilder` not having a Copy/Clone traits implemented, which
are necessary  for constructing an instance to move-capture it  into  a
closure.

Instead,  we mirror `std::fs::DirBuilder` configuration by  storing the
`recursive` and (unix-only) `mode`  parameters locally,  which are then
used to construct an `std::fs::DirBuilder` instance on-the-fly.

This commit also mirrors the (unix-only) DirBuilderExt trait from std.

Fixes: #2369
2020-05-21 12:49:36 +02:00
Alice Ryhl 7cb5e3460c stream: update StreamExt::merge doc (#2520) 2020-05-21 11:54:52 +02:00
Alice Ryhl 9b81580be6 github: update issue templates (#2552) 2020-05-21 09:45:27 +02:00
Geoff Shannon 9b6744cc8e tokio-macros: warn about renaming the tokio dependency (#2521) 2020-05-20 21:50:41 +02:00
Ondřej Hruška 4563699838 codec: add Framed::read_buffer_mut (#2546)
Adds a method to retrieve a mutable reference to the Framed stream's read buffer.
This makes it possible to e.g. externally clear the buffer to prevent the codec from
parsing stale data.
2020-05-20 21:45:03 +02:00
Jake Goulding f48065910e doc: fix two -> to typo (#2527) 2020-05-16 16:31:47 +02:00
ZSL a5c1a7de03 sync: document maximum number of permits (#2539) 2020-05-16 12:41:45 +02:00
Sunjay Varma a343b1d180 Clarifying that Handle::current must be called on a thread managed by tokio (#2493) 2020-05-14 12:28:56 -04:00
Geoff Shannon b44ab27359 docs: improve discoverability of codec module (#2523) 2020-05-14 16:51:55 +02:00
Carl Lerche 02661ba30a chore: prepare v0.2.21 release (#2530) tokio-0.2.21 2020-05-13 11:45:02 -07:00
Carl Lerche fb7dfcf432 sync: use intrusive list strategy for broadcast (#2509)
Previously, in the broadcast channel, receiver wakers were passed to the
sender via an atomic stack with allocated nodes. When a message was
sent, the stack was drained. This caused a problem when many receivers
pushed a waiter node then dropped. The waiter node remained indefinitely
in cases where no values were sent.

This patch switches broadcast to use the intrusive linked-list waiter
strategy used by `Notify` and `Semaphore.
2020-05-12 15:09:43 -07:00
Alice Ryhl a32f918671 chore: change norun to no_run (#2518)
I was building the docs and got the following documentation warning:

warning: unknown attribute `norun`. Did you mean `no_run`?
  --> tokio/src/time/throttle.rs:13:1
   |
13 | / /// Slows down a stream by enforcing a delay between items.
14 | | /// They will be produced not more often than the specified interval.
15 | | ///
16 | | /// # Example
...  |
31 | | /// # }
32 | | /// ```
   | |_______^
   |
   = help: the code block will either not be tested if not marked as a rust one or will be run (which you might not want)
2020-05-12 16:42:24 +02:00
Plecra 221f421464 codec: rewrite of codec::Framed (#2368)
Framed was designed to encapsulate both AsyncRead and AsyncWrite so
that it could wrap two-way connections. It used Fuse to manage the pinned
io object between the FramedWrite and FramedRead structs.

I replaced the Fuse struct by isolating the state used in reading and
writing, and making the code generic over that instead. This means
the FramedImpl struct now has a parameter for the state, and contains
the logic for both directions. The Framed* structs are now simply
wrappers around this type

Hopefully removing the `Pin` handling made things easier to
understand, too.
2020-05-12 13:47:38 +02:00
Jeb Rosen 1cc0168335 macros: disambiguate the built-in #[test] attribute in macro expansion (#2503)
`tokio::test` and related macros now use the absolute path
`::core::prelude::v1::test` to refer to the built-in `test` macro.

This absolute path was introduced in rust-lang/rust#62086.
2020-05-12 09:09:59 +02:00
Patrick Mooney 67220eac37 tokio: add support for illumos target (#2486)
Although very similar in many regards, illumos and Solaris have been
diverging since the end of OpenSolaris.  With the addition of illumos as
a Rust target, it must be wired into the same interfaces which it was
consuming when running under the 'solaris' target.
2020-05-11 22:23:49 +02:00
Boqin QinandAlice Ryhl 3ba818a177 io: add doc warning about concurrently calling poll_read/write_ready (#2439)
Co-authored-by: Alice Ryhl <[email protected]>
Fixes: #2429
2020-05-11 21:59:46 +02:00
Danny Browning 6aeeeff6e8 io: add mio::Ready argument to PollEvented (#2419)
Add additional methods to allow PollEvented to be created with an appropriate
mio::Ready state, so that it can be properly registered with the reactor.

Fixes #2413
2020-05-11 21:47:03 +02:00
Tom Ciborski a75fe38ba5 stream: fix documentation on filter_map (#2511) 2020-05-10 23:08:05 +02:00
Karl Voss adce911b02 doc: add link fragments to CONTRIBUTING.md (#2507)
Added GitHub style link fragments to the `[Commit Squashing]`
sections of CONTRIBUTING.md

Fixes: #2506
2020-05-08 14:40:23 +02:00
zeroed 8565a98601 docs: fix links in tokio::sync (#2491)
Fixes: #2489
2020-05-07 16:26:09 -07:00
Carl Lerche bff21aba6c rt: set task budget after block_in_place call (#2502)
In some cases, when a call to `block_in_place` completes, the runtime is
reinstated on the thread. In this case, the task budget must also be set
in order to avoid starving other tasks on the worker.
2020-05-07 16:25:04 -07:00
Adam C. Foltzer 07533a5255 rt: add Handle::spawn_blocking method (#2501)
This follows a similar pattern to `Handle::spawn` to add the
blocking spawn capabilities to `Handle`.
2020-05-07 16:24:24 -07:00
Carl Lerche 4748b2571f rt: simplify coop implementation (#2498)
Simplifies coop implementation. Prunes unused code, create a `Budget`
type to track the current budget.
2020-05-06 19:02:07 -07:00
Lucio Franco 66fef4a9bc Remove tokio-tls from master (#2497) 2020-05-06 17:30:01 -04:00
Lucio Franco 13e2a366de tls: Deprecate in favor of tokio-native-tls (#2485) 2020-05-06 16:10:57 -04:00
Carl Lerche cc8a662598 sync: simplify the broadcast channel (#2467)
Replace an ad hoc read/write lock with RwLock. Use
The parking_lot RwLock when possible.
2020-05-06 07:37:44 -07:00
Carl Lerche 264ae3bdb2 sync: move CancellationToken tests (#2477)
In preparation of work on `CancellationToken` internals, the tests are
moved into `tests/` and are updated to not depend on internals.
2020-05-03 12:35:47 -07:00
Matthias Einwag 187af2e6a3 sync: add CancellationToken (#2263)
As a first step towards structured concurrency, this change adds a
CancellationToken for graceful cancellation of tasks.

The task can be awaited by an arbitrary amount of tasks due to the usage
of an intrusive list.

The token can be cloned. In addition to this child tokens can be derived.
When the parent token gets cancelled, all child tokens will also get
cancelled.
2020-05-02 14:19:28 -07:00
zeroed 31315b9463 doc: remove reference to the Sink trait in the MPSC documentation (#2476)
The implementation of the Sink trait was removed in 8a7e5778.

Fixes: #2464
Refs: #2389
2020-05-02 13:41:40 -07:00
Eliza Weisman 20b5df9037 task: fix LocalSet having a single shared task budget (#2462)
## Motivation

Currently, an issue exists where a `LocalSet` has a single cooperative
task budget that's shared across all futures spawned on the `LocalSet`
_and_ by any future passed to `LocalSet::run_until` or
`LocalSet::block_on`. Because these methods will poll the `run_until`
future before polling spawned tasks, it is possible for that task to
_always_ deterministically starve the entire `LocalSet` so that no local
tasks can proceed. When the completion of that future _itself_ depends
on other tasks on the `LocalSet`, this will then result in a deadlock,
as in issue #2460.

A detailed description of why this is the case, taken from [this 
comment][1]:

`LocalSet` wraps each time a local task is run in `budget`:
https://github.com/tokio-rs/tokio/blob/947045b9445f15fb9314ba0892efa2251076ae73/tokio/src/task/local.rs#L406

This is identical to what tokio's other schedulers do when running
tasks, and in theory should give each task its own budget every time
it's polled. 

_However_, `LocalSet` is different from other schedulers. Unlike the
runtime schedulers, a `LocalSet` is itself a future that's run on
another scheduler, in `block_on`.  `block_on` _also_ sets a budget:
https://github.com/tokio-rs/tokio/blob/947045b9445f15fb9314ba0892efa2251076ae73/tokio/src/runtime/basic_scheduler.rs#L131

The docs for `budget` state that:
https://github.com/tokio-rs/tokio/blob/947045b9445f15fb9314ba0892efa2251076ae73/tokio/src/coop.rs#L73

This means that inside of a `LocalSet`, the calls to `budget` are
no-ops. Instead, each future polled by the `LocalSet` is subtracting
from a single global budget.

`LocalSet`'s `RunUntil` future polls the provided future before polling
any other tasks spawned on the local set:
https://github.com/tokio-rs/tokio/blob/947045b9445f15fb9314ba0892efa2251076ae73/tokio/src/task/local.rs#L525-L535

In this case, the provided future is `JoinAll`. Unfortunately, every
time a `JoinAll` is polled, it polls _every_ joined future that has not
yet completed. When the number of futures in the `JoinAll` is >= 128,
this means that the `JoinAll` immediately exhausts the task budget. This
would, in theory, be a _good_ thing --- if the `JoinAll` had a huge
number of `JoinHandle`s in it and none of them are ready, it would limit
the time we spend polling those join handles. 

However, because the `LocalSet` _actually_ has a single shared task
budget, this means polling the `JoinAll` _always_ exhausts the entire
budget. There is now no budget remaining to poll any other tasks spawned
on the `LocalSet`, and they are never able to complete.

[1]: https://github.com/tokio-rs/tokio/issues/2460#issuecomment-621403122

## Solution

This branch solves this issue by resetting the task budget when polling
a `LocalSet`. I've added a new function to `coop` for resetting the task
budget to `UNCONSTRAINED` for the duration of a closure, and thus
allowing the `budget` calls in `LocalSet` to _actually_ create a new
budget for each spawned local task. Additionally, I've changed
`LocalSet` to _also_ ensure that a separate task budget is applied to
any future passed to `block_on`/`run_until`.

Additionally, I've added a test reproducing the issue described in
#2460. This test fails prior to this change, and passes after it.

Fixes #2460

Signed-off-by: Eliza Weisman <[email protected]>
2020-04-30 15:19:17 -07:00
Carl Lerche fa9743f0d4 macros: scoped_thread_local should be private (#2470)
Do not export the `scoped_thread_local` macro outside of the Tokio
crate. This is not considered a breaking change as the macro never
worked if used from outside of the crate due to the generated code
referencing crate-private types.
2020-04-30 14:32:47 -07:00
Hanif Ariffin 7a89d66513 io: add get_mut, get_ref and into_inner to Lines (#2450) 2020-04-30 12:44:19 +02:00
Eliza Weisman 45773c5641 mutex: add OwnedMutexGuard for Arc<Mutex<T>>s (#2455)
This PR adds a new `OwnedMutexGuard` type and `lock_owned` and
`try_lock_owned` methods for `Arc<Mutex<T>>`.  This is pretty much the
same as the similar APIs added in #2421. 

I've also corrected some existing documentation that incorrectly
implied that the existing `lock` method cloned an internal `Arc` — I
think this may be a holdover from `tokio` 0.1's `Lock` type?

Signed-off-by: Eliza Weisman <[email protected]>
2020-04-29 15:48:08 -07:00
Matthijs Brobbel c52b78b792 chore: fix a typo (#2461) 2020-04-29 13:06:40 -07:00
Jonathan Foote 1d28060836 chore: add initial security policy (#2360)
Adds an initial security policy based on email discussions with @carllerche,
@hawkw, and co.
2020-04-29 12:37:09 -07:00
Thomas Whiteway 947045b944 time: notify when resetting a Delay to a time in the past (#2290)
If a Delay has been polled, then the task that polled it may be waiting
for a notification.  If the delay gets reset to a time in the past, then
it immediately becomes elapsed, so it should notify the relevant task.
2020-04-29 18:03:44 +02:00
John-John Tedro 2c53bebe56 runtime: mem::forget instead of keeping track of dropped state (#2451) 2020-04-29 08:24:55 -07:00
Carl Lerche 0f4287ac2b chore: prepare v0.2.20 release. (#2458) tokio-0.2.20 2020-04-28 16:32:09 -07:00
Carl Lerche 1bf1928088 rt: fix default thread number logic (#2457)
Previously, the function picking the default number of threads for the
threaded runtime did not factor in `max_threads`. Instead, it only used
the value returned by `num_cpus`. However, if `num_cpus` returns a value
greater than `max_threads`, then the function would panic.

This patch fixes the function by limiting the default number of threads
by `max_threads`.

Fixes #2452
2020-04-28 15:04:41 -07:00
Alice Ryhl a26d3aec96 net: mention that bind sets SO_REUSEADDR (#2454) 2020-04-28 19:44:33 +02:00
Kevin Leimkuhler a819584849 sync: fix slow receivers in broadcast (#2448)
Broadcast uses a ring buffer to store values sent to the channel. In order to
deal with slow receivers, the oldest values are overwritten with new values
once the buffer wraps. A receiver should be able to calculate how many values
it has missed.

Additionally, when the broadcast closes, a final value of `None` is sent to
the channel. If the buffer has wrapped, this value overwrites the oldest
value.

This is an issue mainly in a single capacity broadcast when a value is sent
and then the sender is dropped. The original value is immediately overwritten
with `None` meaning that receivers assume they have lagged behind.

**Solution**

A value of `None` is no longer sent to the channel when the final sender has
been dropped. This solves the single capacity broadcast case by completely
removing the behavior of overwriting values when the channel is closed.

Now, when the final sender is dropped a closed bit is set on the next slot
that the channel is supposed to send to.

In the case of a fast receiver, if it finds a slot where the closed bit is
set, it knows the channel is closed without locking the tail.

In the case of a slow receiver, it must first find out if it has missed any
values. This is similar to before, but must be able to account for channel
closure.

If the channel is not closed, the oldest value may be located at index `n`. If
the channel is closed, the oldest value is located at index `n - 1`.

Knowing the index where the oldest value is located, a receiver can calculate
how many values it may have missed and starts to catch up.

Closes #2425
2020-04-27 21:04:47 -07:00