Limiting memory use of courier-imap with softlimit

I was hit by what seemed to be a bug in courier-imap. It all began a few weeks ago when I tried to synchronize the IMAP account at my home server. The server runs FreeBSD 4.7 and courier-imap. I use Apple's mail client.

Suddenly an operation I perform several times a day failed; the sync hanged at the mailbox selected for storing outgoing mail. Or rather, the client was thrown out and immediately tried to login again and start over with the folder in question. This repeated itself over and over again. The logs on the server didn't give me too much except that the imapd process exited with code 256. I didn't have the time to delve into this problem so I made a backup of the problematic folder and removed it. Also, if there was something with the folder that caused the imap server to barf badly I would have to find out why myself; I didn't want to give away my private messages to someone else.

Today the problem occurred again, this time with a folder containing mails from a mailing list I'm subscribed to. Good, I thought, now I have a folder that cause the server to barf and which I can give away to someone I don't know. Then I realized what the problem was.

When I installed courier-imap I didn't want to use all the tools surrounding the imap daemon. I wanted the daemon to run under daemontools, as most of my other service daemons are. To make this happen I had to go through courier's startup files and scripts to find out how it worked. Now I faintly remembered that the startup script shipped with courier-imap used Bourne Shell's ulimit to limit the maximum allowed size of the imap daemon. Running the daemon with daemontools I had of course gone with softlimit from the same package.

With ulimit you specify limits in kilo bytes while with softlimit you do it in bytes. The daemon was limited to only 65536 bytes of memory when it, with the default installation, would have been limited to a thousand times more. I changed the limit and my broken folders weren't broken anymore.