Repository: alacritty/alacritty (language: rust)

Issue:
EventLoop::new() can panic with "Bad File Descriptor"
We are using Alacritty at Zed to provide the embedded terminal, and across the whole userbase see this panic a few times a day:

```
thread 'main' panicked at 'create mio Poll: Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" }'
/Users/administrator/.cargo/git/checkouts/alacritty-afea874b09a502a5/3330614/alacritty_terminal/src/event_loop.rs:163
<terminal::TerminalBuilder>::new
<project::Project>::create_terminal
// more zed stuff....
```

Although we have struggled to reproduce this problem in situ, we suspect this happens while the filesystem is quite heavily loaded (we're restoring a workspace for example https://github.com/zed-industries/zed/issues/6835). That said, this is not reliably reproducible for those users.

I think the best next step is relatively straightforward, which is to pass the result out of `EventLoop::new()` instead of unwrapping, so that the caller can handle it.

### System

OS: macOS
Version: [Zed](https://zed.dev) using alacritty_terminal v0.21

### Logs

```
thread 'main' panicked at 'create mio Poll: Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" }'
/Users/administrator/.cargo/git/checkouts/alacritty-afea874b09a502a5/3330614/alacritty_terminal/src/event_loop.rs:163
<terminal::TerminalBuilder>::new
<project::Project>::create_terminal
// more zed stuff....
```


Discussion / hints:

Fix patch:
diff --git a/alacritty/src/window_context.rs b/alacritty/src/window_context.rs
index 3f3e6807d6d..891551bbf6b 100644
--- a/alacritty/src/window_context.rs
+++ b/alacritty/src/window_context.rs
@@ -223,7 +223,7 @@ impl WindowContext {
             pty,
             pty_config.hold,
             config.debug.ref_test,
-        );
+        )?;
 
         // The event loop channel allows write requests from the event processor
         // to be sent to the pty loop and ultimately written to the pty.
diff --git a/alacritty_terminal/src/event_loop.rs b/alacritty_terminal/src/event_loop.rs
index 57045f4a300..42025b009d5 100644
--- a/alacritty_terminal/src/event_loop.rs
+++ b/alacritty_terminal/src/event_loop.rs
@@ -67,10 +67,11 @@ where
         pty: T,
         hold: bool,
         ref_test: bool,
-    ) -> EventLoop<T, U> {
+    ) -> io::Result<EventLoop<T, U>> {
         let (tx, rx) = mpsc::channel();
-        EventLoop {
-            poll: polling::Poller::new().expect("create Poll").into(),
+        let poll = polling::Poller::new()?.into();
+        Ok(EventLoop {
+            poll,
             pty,
             tx,
             rx: PeekableReceiver::new(rx),
@@ -78,7 +79,7 @@ where
             event_proxy,
             hold,
             ref_test,
-        }
+        })
     }
 
     pub fn channel(&self) -> EventLoopSender {
@@ -213,8 +214,9 @@ where
             let mut interest = PollingEvent::readable(0);
 
             // Register TTY through EventedRW interface.
-            unsafe {
-                self.pty.register(&self.poll, interest, poll_opts).unwrap();
+            if let Err(err) = unsafe { self.pty.register(&self.poll, interest, poll_opts) } {
+                error!("Event loop registration error: {}", err);
+                return (self, state);
             }
 
             let mut events = Events::with_capacity(NonZeroUsize::new(1024).unwrap());
@@ -235,7 +237,10 @@ where
                 if let Err(err) = self.poll.wait(&mut events, timeout) {
                     match err.kind() {
                         ErrorKind::Interrupted => continue,
-                        _ => panic!("EventLoop polling error: {err:?}"),
+                        _ => {
+                            error!("Event loop polling error: {}", err);
+                            break 'event_loop;
+                        },
                     }
                 }
 


Test patch: