0

I hava a Java server application that monitors a big number of Gmail IMAP folders for new messages.

It's implemented with javamail IdleManager. I have scheduled task that works like this (shortened example)

After a number of runnings I catch a kind of deadlock. Synchronized folder.close(false) hangs forever.

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
props.put("mail.event.scope", "session"); // or "application"
props.put("mail.event.executor", es);
props.setProperty("mail.imaps.usesocketchannels", "true");


// initialized in other place.
// ArrayList<IMAPFolder> folders = new ArrayList<>();
for (IMAPFolder folder : folders) {
    folder.close(false);
}
folders = new ArrayList<>();

// initialized in other place.
// ArrayList<Store> stores = new ArrayList<>();
for (Store store : stores) {
    store.close();
}
stores = new ArrayList<>();


// initialized in other place.
// ExecutorService es = Executors.newCachedThreadPool();
//IdleManager idleManager = new IdleManager(session, es);
es = Executors.newCachedThreadPool();
if (idleManager != null) {
    idleManager.stop();
}
idleManager = new IdleManager(session, es);

//here I run a number of threads in a loop.

Here is the simplified example of these threads.

Store store = connectStore();
IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
stores.add(store);
folders.add(folder);

folder.open(Folder.READ_ONLY);
idleManager.watch(folder);

I'm sure that this is connected to IdleManager, because I've tried my code without idleManager.watch(folder) and everything was ok.

Btw, I've tried this approach of detecting deadlock in Java and it did not show any deadlock.

If I remove the close folder block from the code and close only the stores - the same scenario. store.close() hangs forever just like folder.close(false).

I appreciate any help! Thanks!

Community
  • 1
  • 1
  • I've fixed a number of deadlock bugs related to IdleManager. Are you using the latest JavaMail 1.5.5? – Bill Shannon Jan 16 '16 at 18:42
  • @BillShannon I was using 1.5.4. Now I'm testing the same code with 1.5.5 and every time I do `idleManager.stop(); idleManager = new IdleManager(session, es); idleManager.watch(folder);` I got `javax.mail.MessagingException: IdleManager is not running at com.sun.mail.imap.IdleManager.watch(IdleManager.java:197) ~[javax.mail-1.5.5.jar:1.5.5]` – Kirill Barkunov Jan 18 '16 at 08:33
  • @BillShannon And as I understand it depends on time gap between `idleManager = new IdleManager(session, es); ` and `idleManager.watch(folder);` If I handle this exception and try to `idleManager.watch(folder);` for the second time - everything is ok. – Kirill Barkunov Jan 18 '16 at 08:36
  • IdleManager.stop is not synchronous; it tells the IdleManager thread to stop and then returns immediately. It may take sometime for the thread to top watching all the folders and terminate. I'm not sure why that would result in what you're seeing, however. Why are you stopping the IdleManager and immediately starting a new one? Can you enable [Session debugging](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) in the Session used with IdleManager and post the debug output? – Bill Shannon Jan 19 '16 at 00:55
  • @BillShannon I have a kind of push notification system. Server needs to send push notifications to mobile devices. I did not know how long IdleManager can work without problems. Plus I need to monitor a big dynamic number of Inboxes. Somebody can turn notification off, and there is no way to exclude some folder from IdleManager. So I've decided to reload it every few hours. That's why I need to stop it and re-init again immediately. – Kirill Barkunov Jan 19 '16 at 13:43
  • @BillShannon I'll attach debug log ASAP – Kirill Barkunov Jan 19 '16 at 13:44
  • 1
    IdleManager is intended to be safe to run for long periods of time. You can exclude a Folder from IdleManager by closing the Folder, or by doing some operation on the Folder that requires contacting the server, and then not calling IdleManager.watch at the end of the operation. – Bill Shannon Jan 19 '16 at 20:47
  • @BillShannon I've faced the problem with a "deadlock" today again. And it appears to be just a very-very long action inside Folder. ~3 hours passed before method close() was executed. I can not post log, because it's prod and it's really huge. And Is till can not reproduce it in the simple example. There are around 1500 different Inboxes inside IdleManager. – Kirill Barkunov Jan 21 '16 at 16:24
  • 1
    Are you setting any timeout properties? If you want to debug this further, you can contact me offline at javamail_ww@oracle.com. – Bill Shannon Jan 21 '16 at 19:27

0 Answers0