Google Email. Stopped working, getting "Connection Closed"

Infos:

  • Used Zammad version: 6.1.0-1701845240.5a81e629.bionic
  • Used Zammad installation type: source
  • Operating system: Debian
  • Browser + version: chrome

Expected behavior:

  • Connected Google Email Account pulling emails, as it has been doing for a year.

Actual behavior:

  • Getting error “Connection Closed”

Steps to reproduce the behavior:

  • Reauthenticate

We get the Oauth prompt, selecting the appropriate google email account and accepting. Zammadadds it to the Channel list with hello@ourcompany.com (example), but…

error on the Inbound side:
“Can’t use Channel::Driver::Imap: #<Net::IMAP::Error: connection closed>”

When hitting the edit, where it asks “Keep emails on server”, I get a message in the background “Connection closed”, real fast.

It has been working with the oauth client for a long time, pulling emails in, us responding. Not many requests, our Google Client project shows the stats, stopping around Oct 25.

We thought Oauth issue - seems not to be, we can connect.
or rate limiting. But we don’t have many.
IP blocked? why? how do I verify?

We are on linode, any special ports being used by Zammad in this google integration?

FYI: we also got a few regular imap and smtp through sendgrid setup, those works, it’s just the google email not working anymore.

Before trying anything else… you might want to update Zammad to the latest stable version. This would be 6.5.2 right now. Tons of bug fixes and some security related fixes (apart from great features / additions!) on the way.

will do. However, we didn’t upgrade and yet it stopped. Normally “doing something, breaks something”, so I’m hopeful for other insights or things to consider looking at.

The solution was forcing ip v4 on the server.

long story, but after 24h it suddenly worked again, then after just an hour, didn’t anymore. We tried reproduce and it was hard. On an hosted install, everything worked.

Doing an experiment, forcing ip v4 to be used, not ip v6, it worked.

irb(main):001:0> ch = Channel.find(17)
=> 
#<Channel:0x00007fc95233b558                                                    
...                                                                             
irb(main):002:0> ch.fetch
fetching imap (imap.gmail.com/hello@fasttravel.com port=993,ssl=true,starttls=false,folder=INBOX,keep_on_server=true,auth_type=XOAUTH2)
Can't use Channel::Driver::Imap: #<Net::IMAP::Error: connection closed>         
#<Net::IMAP::Error: connection closed>                                          
=> false     

EXPERIMENT: MONKEYPATCH forcing ipv4

irb(main):044:0> require 'socket'
=> false
irb(main):045:0> require 'net/imap'
=> false
irb(main):046:0> 
irb(main):047:1* module Net
irb(main):048:2*   class IMAP
irb(main):049:2*     private
irb(main):050:3*     def tcp_socket(host, port)
irb(main):051:3*       # Resolve IPv4 address only
irb(main):052:3*       addr = Addrinfo.getaddrinfo(host, port, Socket::AF_INET, Socket::SOCK_STREAM).first
irb(main):053:3*       sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
irb(main):054:3*       sock.connect(addr.to_sockaddr)
irb(main):055:3*       sock
irb(main):056:2*     end
irb(main):057:1*   end
irb(main):058:0> end
=> :tcp_socket
irb(main):059:0> imap = Net::IMAP.new('imap.gmail.com', 993, true)
=> 
#<Net::IMAP:0x00007f36ac9c46d8
...
irb(main):060:0> puts imap.capability.inspect
["IMAP4REV1", "UNSELECT", "IDLE", "NAMESPACE", "QUOTA", "ID", "XLIST", "CHILDREN", "X-GM-EXT-1", "XYZZY", "SASL-IR", "AUTH=XOAUTH2", "AUTH=PLAIN", "AUTH=PLAIN-CLIENTTOKEN", "AUTH=OAUTHBEARER"]
=> nil
irb(main):061:0> imap.disconnect
=> nil
irb(main):062:0> ch.fetch
(irb):62:in `<main>': undefined local variable or method `ch' for main:Object (NameError)
irb(main):063:0> ch = Channel.find(17)
=> 
#<Channel:0x00007f36ad47bf40                                                                          
...                                                                                                   
irb(main):064:0> ch.fetch
fetching imap (imap.gmail.com/hello@fasttravel.com port=993,ssl=true,starttls=false,folder=INBOX,keep_on_server=true,auth_type=XOAUTH2)
 - no message                                                                                         
=> true       

The more permanent fix was changing /etc/gai.conf on the server:

1) Prefer IPv4 system-wide (recommended)
sudo nano /etc/gai.conf
# uncomment or add:
precedence ::ffff:0:0/96  100

Apply + restart Zammad:

sudo systemctl restart systemd-resolved || true
sudo systemctl restart networking || true
sudo systemctl restart zammad-worker zammad-web

Confirm resolution order:

getent ahosts imap.gmail.com   # IPv4 should appear before IPv6