Skip to content

fix(email): guard against IndexError when IMAP search returns empty list - #2289

Closed
Mibayy wants to merge 1 commit into
NousResearch:mainfrom
Mibayy:fix/email-imap-indexerror
Closed

fix(email): guard against IndexError when IMAP search returns empty list#2289
Mibayy wants to merge 1 commit into
NousResearch:mainfrom
Mibayy:fix/email-imap-indexerror

Conversation

@Mibayy

@Mibayy Mibayy commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Summary

imap.uid('search') returns data=[] when the mailbox is empty or has no matching messages. Both call sites accessed data[0] without checking whether the list was non-empty first, causing IndexError: list index out of range.

Changes

gateway/platforms/email.py:

  • Line 233 (connect method, ALL search): if status == "OK" and data[0]:if status == "OK" and data and data[0]:
  • Line 298 (_fetch_messages method, UNSEEN search): if status != "OK" or not data[0]:if status != "OK" or not data or not data[0]:

Why

The IMAP spec allows search to succeed (status == "OK") with an empty result set (data == []). A fresh inbox or an inbox with no unread messages hits this path every time.

Closes #2137

imap.uid('search') can return data=[] when the mailbox is empty or
has no matching messages. Accessing data[0] without checking len first
raises IndexError: list index out of range.

Fixed at both call sites in gateway/platforms/email.py:
- Line 233 (connect): ALL search on startup
- Line 298 (fetch): UNSEEN search in the polling loop

Closes NousResearch#2137
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #2466 — cherry-picked with authorship preserved. Thanks!

@teknium1 teknium1 closed this Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(email): IndexError when IMAP search returns empty data list

2 participants