# Notes + Interviews on Fallible Collection Allocations

**URL:** <https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619>\
**Category:** Uncategorized\
**Created:** [July 26, 2017, 1:59pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619 "2017-07-26T13:59:52Z")\
**Posts on this page:** 14\
**Page:** 3

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [August 2, 2017, 9:44am UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/41 "2017-08-02T09:44:44Z")

</div>

I have another generics based idea for solving the problem. We change all methods that can fail due to oom to have a generic parameter of an `AllocFailure` trait, which is defaulted to give the current experience:

```rust
#![feature(default_type_parameter_fallback, associated_type_defaults)]
trait OomFailure<ReturnTy> {
    type ResultType = ReturnTy;
}

struct AbortOnOom;
impl<T> OomFailure<T> for AbortOnOom {}

struct OomResult;
struct OomError;
impl<T> OomFailure<T> for OomResult {
    type ResultType = Result<T, OomError>;
}

struct Vec<T>(T);

impl<T> Vec<T> {
    fn push<AF: OomFailure<()> = AbortOnOom>(&mut self, t: T) -> AF::ResultType {
        unimplemented!()
    }
}

fn foo<T>(v: Vec<T>, u: T, w: T) {
    v.push(u); // doesn't work yet due to https://github.com/rust-lang/rust/issues/36887#issuecomment-296787518 being unresolved
    v.push::<AbortOnOom>(u); // the above line should work just like this one
    v.push::<OomResult>(w); // warning: unused `std::result::Result` which must be used
}

```

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [August 3, 2017, 6:46pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/42 "2017-08-03T18:46:18Z")

</div>

I think the server aspect of this discussion is under-appreciated. Note that none of the people you interviewed are working on servers. Embedded is not quite the same.

I think there are at least three different types of servers with different memory uses:

- Request-based (think web application): many concurrent requests with individually low memory use. Technically you might be able to isolate the effects of memory allocation failure between individual requests.
- Classic RDBMS: a long-running process that uses as much memory as possible. A process should never die.
- Big Data: a (relatively) short-running process that uses as much memory as possible. A process abort can be handled gracefully but should be avoided as it increases overall computation time.

I can put you in touch with a Spark developer if you’re interested.

---

<div class="post-metadata">

**Author:** ![lilith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lilith/32/11080_2.png) [@lilith](https://internals.rust-lang.org/u/lilith)\
**Post date:** [August 3, 2017, 7:03pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/43 "2017-08-03T19:03:34Z")

</div>

The pushback on fallible allocations is incredibly bizarre to me.

Graceful handling is the status quo with C libraries, and Rust is supposed to be a systems programming language with a high level of control over memory. I was shocked to find Rust lacking in this when I first started using it full-time, and a year and a half later the same flawed arguments against proper allocation keep returning.

I’m questioning what Rust is even for, now.

---

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [August 3, 2017, 7:08pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/44 "2017-08-03T19:08:52Z")

</div>

There’s been some discussion of isolating per-request (or per-work unit) failures by having allocation panic (via unwinding) and threads catch the unwind. We should keep in mind that this only works for the thread-per-task model, but not for an event loop model (ie, used in async IO) with worker threads. For that, you actually need normal fallible allocation that returns an error when allocation can’t be performed.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [August 4, 2017, 9:59am UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/45 "2017-08-04T09:59:54Z")

</div>

> [@joshlf](#):
>
> We should keep in mind that this only works for the thread-per-task model, but not for an event loop model (ie, used in async IO) with worker threads. For that, you actually need normal fallible allocation that returns an error when allocation can’t be performed.

Depends on how expensive panic isolation is. Potentially something like `tokio` could wrap every single invocation into a `Task` and fail that `Task` as a whole if it panics. If you have something like a web server then it could isolate the request processing into a separate `Task` from the overall request handling which would allow returning a 500 on allocation failure during the actual processing stage.

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [August 4, 2017, 3:47pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/46 "2017-08-04T15:47:28Z")

</div>

Unwinding is not a panacea. Depending on unwinding for continuity makes it so that you can’t use `std::sync::{Mutex, Once, RwLock}` anywhere in your code because of `std::sync::PoisonError`.

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [August 14, 2017, 8:22pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/47 "2017-08-14T20:22:18Z")

</div>

> [@joshlf](#):
>
> There’s been some discussion of isolating per-request (or per-work unit) failures by having allocation panic (via unwinding) and threads catch the unwind. We should keep in mind that this only works for the thread-per-task model, but not for an event loop model (ie, used in async IO) with worker threads. For that, you actually need normal fallible allocation that returns an error when allocation can’t be performed.

Can you elaborate on the problems? It seems like you just tack .catch\_unwind() onto your futures and call it a day? (outside my area of expertise by a mile)

---

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [August 15, 2017, 5:08am UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/48 "2017-08-15T05:08:24Z")

</div>

> Can you elaborate on the problems? It seems like you just tack .catch\_unwind() onto your futures and call it a day? (outside my area of expertise by a mile)

Sure. Two issues that I see: performance and unwinding. I benchmarked it, and the performance overhead of `catch_unwind` isn't bad, but it's definitely worse than just matching on a `Result` (I forget the exact numbers - this was about a week ago). Also, this assumes that the panic strategy is unwind - it doesn't work for `panic = abort`.

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [August 15, 2017, 1:51pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/49 "2017-08-15T13:51:29Z")

</div>

How is this different than using threads? I don’t see what’s unique about futures here.

---

<div class="post-metadata">

**Author:** ![joshlf](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshlf/32/3815_2.png) [@joshlf](https://internals.rust-lang.org/u/joshlf)\
**Post date:** [August 15, 2017, 3:55pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/50 "2017-08-15T15:55:58Z")

</div>

I suppose that the `panic = abort` vs `panic = unwind` issue isn’t different - you’re right. For the performance issue, I’m thinking that in the threaded model, you have some outer loop that loops acquiring more work to do and then doing it, and the `catch_unwind` is called in a parent function of that loop, so the check is only performed either when the thread is quitting normally or when there’s been an unwinding panic. On the other hand, since in an event loop-based system, the unit of work that you’d want to restart on a panic is a single future/async execution, you’d need to catch at that granularity, and thus you’d need to do `catch_panic` around each of those executions, checking whether you were panicking each time you were about to switch to do a different unit of work.

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [August 18, 2017, 3:36am UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/51 "2017-08-18T03:36:40Z")

</div>

Based on the discussion and some others I’ve had elsewhere I’ve posted the following RFC: [https://github.com/rust-lang/rfcs/pull/2116](https://github.com/rust-lang/rfcs/pull/2116)

It ain’t perfect, but to be blunt I’m exhausted and this problem is _awful_.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [August 23, 2017, 12:32pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/52 "2017-08-23T12:32:18Z")

</div>

> [@jethrogb](#):
>
> Unwinding is not a panacea. Depending on unwinding for continuity makes it so that you can’t use std::sync::{Mutex, Once, RwLock} anywhere in your code because of std::sync::PoisonError.

Could you elaborate? Poisoning actually _helps_ dealing with unwinding; if it wasn't for poisoning, unwinding would be much more likely to introduce subtle bugs into programs. So it actually seems to me like _especially_ when you do unwinding should you use concurrency primitives that do poisoning.

If you don't do unwinding, things will never be poisoned anyway.

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [August 23, 2017, 5:30pm UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/53 "2017-08-23T17:30:10Z")

</div>

I’ve responded to your question [on GitHub](https://github.com/rust-lang/rfcs/pull/2116#issuecomment-324406227) to keep the discussion in one place.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:28am UTC](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619/54 "2019-03-25T08:28:48Z")

</div>

Closed on March 25, 2019, 8:28am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/notes-interviews-on-fallible-collection-allocations/5619.md?page=2)
