# Insufficient \`std::io::Error\`

**URL:** <https://internals.rust-lang.org/t/insufficient-std-io-error/3597>\
**Category:** libs\
**Created:** [June 13, 2016, 8:13pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597 "2016-06-13T20:13:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![troplin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/troplin/32/1182_2.png) [@troplin](https://internals.rust-lang.org/u/troplin)\
**Post date:** [June 13, 2016, 8:13pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/1 "2016-06-13T20:13:50Z")

</div>

IMO the `std::io::Error` type is not sufficent especially to handle `accept()` failures correctly.

The current `std::io::ErrorKind::Other` variant mixes fatal and nonfatal errors and there’s no possibility to decide (except for parsing the message which is obviously not portable).

## Errors that can be returned by `accept` on linux:

### Fatal errors

- `EBADF` =\> `ErrorKind::Other`
- `EINVAL` =\> `ErrorKind::InvalidInput`
- `EOPNOTSUPP` =\> `ErrorKind::Other`
- `ENOTSOCK` =\> `ErrorKind::Other`
- `EFAULT` =\> `ErrorKind::Other`

All of those should be panics IMO.

### Semi-transient errors/unclear

- `EPERM` =\> `ErrorKind::PermissionDenied`
- `EMFILE` =\> `ErrorKind::Other`
- `ENFILE` =\> `ErrorKind::Other`
- `ENOBUFS`, `ENOMEM` =\> `ErrorKind::Other`
- `EPROTO` =\> `ErrorKind::Other`

### Transient errors

- `EAGAIN` or `EWOULDBLOCK` =\> `ErrorKind::WouldBlock`
- `ECONNABORTED` =\> `ErrorKind::ConnectionAborted`
- `EINTR` =\> `ErrorKind::Interrupted`

According to the man pages, it’s also possible that errors can be returned, that are already pending for the new socket, those should be treated like `EAGAIN`, i.e. they are nonfatal. Many of those are also `ErrorKind::Other`.

That means, that it’s currently impossible to handle this correctly… `ErrorKind::Other` could mean anything.

Is there anything that can be done to clean this up a bit?

---

<div class="post-metadata">

**Author:** ![troplin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/troplin/32/1182_2.png) [@troplin](https://internals.rust-lang.org/u/troplin)\
**Post date:** [June 13, 2016, 8:21pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/2 "2016-06-13T20:21:57Z")

</div>

Sorry for the noise, I have to think a bit more about this before posting…

EDIT: Restored the original post (edited a little bit)

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [June 13, 2016, 10:18pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/3 "2016-06-13T22:18:46Z")

</div>

Could you talk more concretely about specifically which errors you want to handle and what “handling” them means?

You can get the raw errno value out of an io::Error, by the way: [https://doc.rust-lang.org/std/io/struct.Error.html#method.raw\_os\_error](https://doc.rust-lang.org/std/io/struct.Error.html#method.raw_os_error)

---

<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:** [June 13, 2016, 10:44pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/4 "2016-06-13T22:44:48Z")

</div>

Sounds to me like your problem would be mostly solved if you could distinguish between the variety of resource-exhausted errors and other `Other` errors, plus perhaps a change from some of the `Other` errors to `InvalidInput`? That last one might be hard, since different error values can mean different things for different system calls and different platforms. So if you want to be true to a particular OS, you’ll need to interpret the raw code regardless (and then no longer be platform-independent).

---

<div class="post-metadata">

**Author:** ![troplin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/troplin/32/1182_2.png) [@troplin](https://internals.rust-lang.org/u/troplin)\
**Post date:** [June 14, 2016, 5:30pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/5 "2016-06-14T17:30:41Z")

</div>

> [@sfackler](#):
>
> You can get the raw errno value out of an io::Error, by the way: [https://doc.rust-lang.org/std/io/struct.Error.html#method.raw\_os\_error](https://doc.rust-lang.org/std/io/struct.Error.html#method.raw_os_error)

Whoa, how could I miss that. That's a bit embarassing now...

> [@sfackler](#):
>
> Could you talk more concretely about specifically which errors you want to handle and what "handling" them means?

Specifically, on OS X the default maximum file handles is ridiculously low (256). I've run into this (`ENFILE`, or was it `EMFILE..`) and took it as a motivation to improve the error handling generally.

Previously I just handled all unknown errors as fatal, but here I can obviously just go on and once there are enough free file handles, it will all be back to normal. Same is true for other kind of errors, at least on linux apparently:

```
Error handling
   Linux accept() (and accept4()) passes already-pending network errors
   on the new socket as an error code from accept(). This behavior
   differs from other BSD socket implementations. For reliable
   operation the application should detect the network errors defined
   for the protocol after accept() and treat them like EAGAIN by
   retrying. In the case of TCP/IP, these are ENETDOWN, EPROTO,
   ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and
   ENETUNREACH.

```

OTOH I'm very hesitant to just retry on any unknown error, I fear that this will "end" in an endless loop eating up all my CPU.

Using the raw system error code is a last resort but it's also not ideal. I think it should be possible to handle this in a robust way by just using the standard library without additionally depending on `libc` or `nix` (for the error constants).

> [@jethrogb](#):
>
> That last one might be hard, since different error values can mean different things for different system calls and different platforms. So if you want to be true to a particular OS, you'll need to interpret the raw code regardless (and then no longer be platform-independent).

Ideally the standard library would provide a classification of the error depending on the system call and the platform.

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [June 14, 2016, 7:58pm UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/6 "2016-06-14T19:58:13Z")

</div>

I’m a bit suspicious that anyone actually pays attention to that paragraph of the man page 😛

---

<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:26am UTC](https://internals.rust-lang.org/t/insufficient-std-io-error/3597/7 "2019-03-25T08:26:19Z")

</div>

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

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