Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link

Copilot AI commented Nov 3, 2025

Fix reproducible freeze with "imap_peek = no" + "color index"

Problem

When imap_peek = no is set and a color index pattern is configured, NeoMutt freezes on "Fetching message..." when a new email arrives.

Root Cause

The issue is in conn/gnutls.c where gnutls_record_recv(), gnutls_record_send(), and gnutls_handshake() can return GNUTLS_E_AGAIN when the socket would block, but the code loops indefinitely without checking if data becomes available.

Solution

Modified three locations in conn/gnutls.c to poll the socket with the configured socket_timeout when GNUTLS_E_AGAIN is returned:

  1. tls_socket_read() - Fixed the main issue affecting message fetching
  2. tls_socket_write() - Applied the same fix for consistency
  3. tls_negotiate() - Applied the same fix to the handshake for robustness

Implementation Details

  • Created helper function tls_socket_poll_with_timeout() to eliminate code duplication
  • Function returns bool (true = ready, false = timeout/error) for clarity
  • When GNUTLS_E_AGAIN is returned, the code now polls the socket with timeout
  • If poll times out or fails, an error is reported and false is returned
  • If poll succeeds, the operation is retried
  • Respects the existing socket_timeout configuration (default 30 seconds)
  • Uses C-style comments for consistency with the codebase
  • Removed accidental CodeQL artifact symlink
  • Updated function documentation to accurately reflect its generic purpose
  • Fixed build error by moving helper function definition before its first use

Testing & Validation

  • Code compiles successfully with GnuTLS
  • No security vulnerabilities detected by CodeQL
  • Code review feedback addressed (extracted helper function, removed artifact, updated docs, fixed build, simplified to bool)
  • Changes follow the same pattern as OpenSSL implementation
  • Minimal, surgical changes to fix the issue

Security Summary

No security vulnerabilities were introduced or discovered during this fix. CodeQL analysis passed with zero alerts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Reproducible freeze with "imap_peek = no" + "color index"</issue_title>
<issue_description>## NeoMutt Version

NeoMutt 20250905-3-ea2d1c
Copyright (C) 2015-2025 Richard Russon and friends
NeoMutt comes with ABSOLUTELY NO WARRANTY; for details type 'neomutt -vv'.
NeoMutt is free software, and you are welcome to redistribute it
under certain conditions; type 'neomutt -vv' for details.

System: Linux 6.14.0-27-generic (x86_64)
ncurses: ncurses 6.5.20250216 (compiled with 6.5.20250216)
libidn2: 2.3.8 (compiled with 2.3.8)
OpenSSL: OpenSSL 3.4.1 11 Feb 2025
libnotmuch: 5.6.0
storage: tokyocabinet

Configure options: --ssl --sasl --enable-notmuch --enable-debug --tokyocabinet --asan --prefix=/usr/local

Compilation CFLAGS: -std=c11 -fno-delete-null-pointer-checks -D_ALL_SOURCE=1 -D_GNU_SOURCE=1 -D__EXTENSIONS__ -D_XOPEN_SOURCE_EXTENDED -I/usr/local/include -DNCURSES_WIDECHAR -fsanitize=address -fno-omit-frame-pointer -fno-common -g -O0

Compile options:
  -autocrypt +fcntl -flock -fmemopen +futimens +getaddrinfo -gnutls -gpgme 
  -gsasl -gss +hcache -homespool +idn +inotify -locales_hack -lua +nls +notmuch 
  +openssl +pgp +regex +sasl +smime -sqlite +truecolor 

Devel options:
  asan 

MAILPATH="/var/mail"
PKGDATADIR="/usr/local/share/neomutt"
SENDMAIL="/usr/sbin/sendmail"
SYSCONFDIR="/usr/local/etc"

To learn more about NeoMutt, visit: https://neomutt.org
If you find a bug in NeoMutt, please raise an issue at:
    https://github.com/neomutt/neomutt/issues
or send an email to: <[email protected]>

Expected Behaviour

Continue using NeoMutt after a new email arrives.

Actual Behaviour

When checking email, if a new email arrives, NeoMutt freezes on "Fetching message...".

Steps to Reproduce

  1. Adapt the following minimal RC according to your account details:
# set up of my account
set realname = 'Scott Kostyshak'
set imap_user = '[email protected]'
set imap_pass = "fakepass"
set folder = "~/.mail"
set my_cache_parent = $folder
set smtp_url = "smtp://[email protected]:587/"
set smtp_pass = $imap_pass
set folder = "imaps://imap.gmail.com:993"
set spoolfile = +INBOX

# just speeds up the reproducing
set timeout = 10

# Both of the below are necessary for me to reproduce.
set imap_peek = no
color index brightred default '~h "^(To|Cc):.*,"'
  1. Start NeoMutt

  2. Send email to yourself.

  3. Wait about 10 seconds (this is related to the "set timeout" rc config in the minimal rc.

Once the new email is recognized by NeoMutt, then it gets stuck on "Fetching message...".

How often does this happen?

Always

  • Is your email local (maildir) or remote (IMAP)?

remote (IMAP)

I'm on Ubuntu 25.04.

Here is the debug log (level 1), in case it is useful:

debug-log.txt</issue_description>

<agent_instructions>Try to understand the root cause and prepare a fix.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@flatcap ``` main.c:1758: main (argc=3, argv=0x7fff336703a8, envp=0x7fff336703c8) index/dlg_index.c:1272: dlg_index (dlg=0x973ef10, m_init=0x97526e0) gui/mutt_window.c:608: window_redraw (win=0x9733990) gui/mutt_window.c:570: window_repaint (win=0xa061190) gui/mutt_window.c:564: window_repaint (win=0x9773150) index/index.c:635: index_repaint (win=0x9773150) menu/draw.c:336: menu_redraw_index (menu=0xa072a60) index/dlg_index.c:942: index_color (menu=0xa072a60, line=2530) index/dlg_index.c:1417: email_set_color (m=0x97526e0, e=0xa11dc70) pattern/exec.c:1152: mutt_pattern_exec (pat=0x9758c10, flags=1 '\001', m=0x97526e0, e=0xa11dc70, cache=0x7fff3366fc90) mx.c:1151: mx_msg_open (m=0x97526e0, e=0xa11dc70) imap/message.c:2037: imap_msg_open (m=0x97526e0, msg=0xa182da0, e=0xa11dc70) imap/command.c:1158: imap_cmd_step (adata=0x9766580) conn/socket.c:245: mutt_socket_readln_d(buf=0x973d210 "a0050 OK Fetch completed (0.006 + 0.000 + 0.005 secs).", buflen=512, conn=0x973bef0, dbg=5) conn/socket.c:206: mutt_socket_readchar(conn=0x973bef0, c=0x7fff3366f6d3 "\t") conn/zstrm.c:147: zstrm_read (conn=0x973bef0, buf=0x973c194 "F..."..., len=1024) conn/gnutls.c:1061: tls_socket_read (conn=0x978d640, buf=0x9790da0 "\273...", count=8192) ```

It gets stuck in tls_socket_read().
gnutls_record_recv() times out and returns GNUTLS_E_AGAIN, so we keep looping.</comment_new>
<comment_new>@scottkosty
Thanks for figuring that out! Let me know if there's any other testing I can do that would be helpful.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Reproducible freeze with "imap_peek = no" + "color index" Fix infinite loop in GnuTLS socket I/O when socket blocks Nov 3, 2025
Copilot AI requested a review from roccoblues November 3, 2025 11:36
Copilot AI requested a review from roccoblues November 3, 2025 12:29
@roccoblues roccoblues marked this pull request as ready for review November 3, 2025 12:30
Copy link
Member

@roccoblues roccoblues left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@roccoblues
Copy link
Member

@copilot we have failing builds. Please check.

Copy link
Author

Copilot AI commented Nov 3, 2025

@copilot we have failing builds. Please check.

Fixed in commit e696219. The helper function tls_socket_poll_with_timeout() was being called before it was defined, causing a build error. Moved the function definition before tls_negotiate() where it's first used.

@flatcap flatcap requested a review from gahr November 3, 2025 13:12
@flatcap
Copy link
Member

flatcap commented Nov 3, 2025

@gahr
This PR fixes the freezing problem, but a third opinion would be nice :-)

Copy link
Author

Copilot AI commented Nov 3, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copilot AI requested a review from flatcap November 3, 2025 13:38
Co-authored-by: Dennis Schön <[email protected]>
Co-authored-by: Pietro Cerutti <[email protected]>
Assisted-by: GitHub Copilot
@flatcap flatcap force-pushed the copilot/fix-16376763-48345674-a15012fe-63df-4972-92d4-6cf828064b81 branch from c54c361 to 9ea1dfe Compare November 10, 2025 15:51
@flatcap flatcap merged commit 9ea1dfe into main Nov 10, 2025
13 checks passed
@flatcap flatcap deleted the copilot/fix-16376763-48345674-a15012fe-63df-4972-92d4-6cf828064b81 branch November 10, 2025 16:07
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.

Reproducible freeze with "imap_peek = no" + "color index"

4 participants