Index: apps/app_voicemail.c =================================================================== --- apps/app_voicemail.c (revision 311843) +++ apps/app_voicemail.c (working copy) @@ -7785,6 +7785,8 @@ static int close_mailbox(struct vm_state *vms, struct ast_vm_user *vmu) { int x = 0; + int count = 0; + #ifndef IMAP_STORAGE int res = 0, nummsg; char fn2[PATH_MAX]; @@ -7801,8 +7803,11 @@ return ERROR_LOCK_PATH; } + /* update count as message may have arrived while we've got mailbox open */ + count = count_messages(vmu, vms->curdir); + /* must check up to last detected message, just in case it is erroneously greater than maxmsg */ - for (x = 0; x < vms->lastmsg + 1; x++) { + for (x = 0; x < count; x++) { if (!vms->deleted[x] && ((strcasecmp(vms->curbox, "INBOX") && strcasecmp(vms->curbox, "Urgent")) || !vms->heard[x] || (vms->heard[x] && !ast_test_flag(vmu, VM_MOVEHEARD)))) { /* Save this message. It's not in INBOX or hasn't been heard */ make_file(vms->fn, sizeof(vms->fn), vms->curdir, x);