diff -r 51f5680d4108 src/lib-index/mail-cache-compress.c
--- a/src/lib-index/mail-cache-compress.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib-index/mail-cache-compress.c	Tue Oct 07 19:09:47 2014 +0300
@@ -112,8 +112,16 @@
 	ext = mail_index_view_get_ext(view, cache->ext_id);
 	file_seq = ext != NULL ? ext->reset_id + 1 : (uint32_t)ioloop_time;
 
-	if (cache->hdr != NULL && file_seq <= cache->hdr->file_seq)
+	if (ext == NULL)
+		i_debug("%s compress doesn't have ext, using file_seq=ioloop_time=%u", cache->filepath, file_seq);
+	else
+		i_debug("%s has ext, using file_seq=reset_id+1 = %u", cache->filepath, file_seq);
+
+	i_debug("%s hdr.file_seq=%u", cache->filepath, cache->hdr == NULL ? -1U : cache->hdr->file_seq);
+	if (cache->hdr != NULL && file_seq <= cache->hdr->file_seq) {
 		file_seq = cache->hdr->file_seq + 1;
+		i_debug("%s increasing file_seq=%u because old is %u", cache->filepath, file_seq, cache->hdr->file_seq);
+	}
 	mail_index_view_close(&view);
 
 	return file_seq != 0 ? file_seq : 1;
@@ -339,6 +347,7 @@
 		array_free(&ext_offsets);
 		return -1;
 	}
+	i_debug("%s compress finished size=%u inode=%ld file_seq=%u", cache->filepath, (int)st.st_size, (long)st.st_ino, file_seq);
 	if (rename(temp_path, cache->filepath) < 0) {
 		mail_cache_set_syscall_error(cache, "rename()");
 		array_free(&ext_offsets);
@@ -353,6 +362,7 @@
 		if (offsets[i] != 0) {
 			mail_index_update_ext(trans, i + 1, cache->ext_id,
 					      &offsets[i], &old_offset);
+			i_debug("%s compress updates cache seq=%u offset=%u old_offset=%u", cache->filepath, i+1, offsets[i], old_offset);
 		}
 	}
 	array_free(&ext_offsets);
@@ -392,6 +402,7 @@
 		if (ret >= 0) {
 			if (ret == 0)
 				return 0;
+			i_debug("%s exists now, hdr.file_seq=%u need_compress_file_seq=%u", cache->filepath, hdr.file_seq, cache->need_compress_file_seq);
 			if (cache->need_compress_file_seq == 0) {
 				/* previously it didn't exist or it
 				   was unusable and was just unlinked */
@@ -440,6 +451,7 @@
 		if (ret < 0)
 			return -1;
 
+		i_debug("%s just compressed", cache->filepath);
 		/* was just compressed, forget this */
 		cache->need_compress_file_seq = 0;
 		file_dotlock_delete(dotlock_r);
@@ -451,6 +463,7 @@
 
 		return mail_cache_reopen(cache) < 0 ? -1 : 0;
 	}
+	i_debug("%s is being compressed", cache->filepath);
 
 	/* we want to recreate the cache. write it first to a temporary file */
 	fd = mail_index_create_tmp_file(cache->index, cache->filepath, &temp_path);
diff -r 51f5680d4108 src/lib-index/mail-cache-fields.c
--- a/src/lib-index/mail-cache-fields.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib-index/mail-cache-fields.c	Tue Oct 07 19:09:47 2014 +0300
@@ -500,6 +500,8 @@
 		    sizeof(uint32_t));
 	ret = mail_cache_write(cache, buffer->data, buffer->used,
 			       offset + MAIL_CACHE_FIELD_LAST_USED());
+	i_debug("%s writing last-used at=%u size=%u", cache->filepath, 
+		(int)(offset + MAIL_CACHE_FIELD_LAST_USED()), (int)buffer->used);
 	if (ret == 0) {
 		buffer_set_used_size(buffer, 0);
 		copy_to_buf_byte(cache, buffer, FALSE,
@@ -507,6 +509,7 @@
 
 		dec_offset = offset +
 			MAIL_CACHE_FIELD_DECISION(cache->file_fields_count);
+		i_debug("%s writing cache decisions at=%u size=%u", cache->filepath, dec_offset, (int)buffer->used);
 		ret = mail_cache_write(cache, buffer->data, buffer->used,
 				       dec_offset);
 		if (ret == 0) {
diff -r 51f5680d4108 src/lib-index/mail-cache-transaction.c
--- a/src/lib-index/mail-cache-transaction.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib-index/mail-cache-transaction.c	Tue Oct 07 19:09:47 2014 +0300
@@ -122,6 +122,7 @@
 		ctx->cache->hdr->file_seq;
 	mail_index_ext_set_reset_id(ctx->trans, ctx->cache->ext_id,
 				    ctx->cache_file_seq);
+	i_debug("%s cache transaction reset set reset_id=%u", ctx->cache->filepath, ctx->cache_file_seq);
 
 	if (ctx->cache_data != NULL)
 		buffer_set_used_size(ctx->cache_data, 0);
@@ -179,11 +180,13 @@
 	view = mail_index_view_open(cache->index);
 	trans = mail_index_transaction_begin(view,
 					MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL);
+	i_debug("%s compressing", cache->filepath);
 	if (mail_cache_compress(cache, trans, &lock) < 0) {
 		mail_index_transaction_rollback(&trans);
 		ret = -1;
 	} else {
 		ret = mail_index_transaction_commit(&trans);
+		i_debug("%s transaction committed", cache->filepath);
 		if (lock != NULL)
 			mail_cache_compress_unlock(&lock);
 	}
@@ -325,6 +328,8 @@
 	   synced. */
 	recs = array_get(&ctx->cache_data_seq, &seq_count);
 	for (i = 0; i < seq_count; i++) {
+		i_debug("%s cache index: seq=%u reset_id=%u write_offset=%u", cache->filepath, recs[i].seq,
+			ctx->cache_file_seq, (int)write_offset);
 		mail_index_update_ext(ctx->trans, recs[i].seq, cache->ext_id,
 				      &write_offset, NULL);
 
@@ -439,6 +444,7 @@
 		ret = -1;
 	else {
 		/* update records' cache offsets to index */
+		i_debug("%s append flush contents at=%u size=%u", ctx->cache->filepath, write_offset, (int)ctx->last_rec_pos);
 		ctx->records_written++;
 		ret = mail_cache_transaction_update_index(ctx, write_offset);
 	}
@@ -450,6 +456,7 @@
 		    ctx->cache_data, ctx->last_rec_pos, (size_t)-1);
 	buffer_set_used_size(ctx->cache_data,
 			     ctx->cache_data->used - ctx->last_rec_pos);
+	i_debug("%s cache buffer new size=%u", ctx->cache->filepath, (int)ctx->cache_data->used);
 	ctx->last_rec_pos = 0;
 	ctx->min_seq = 0;
 
@@ -468,6 +475,8 @@
 	rec = PTR_OFFSET(data, ctx->last_rec_pos);
 	rec->size = size - ctx->last_rec_pos;
 	i_assert(rec->size > sizeof(*rec));
+	i_debug("%s cache update last_rec_pos=%u size=%u", ctx->cache->filepath,
+		(int)ctx->last_rec_pos, (int)rec->size);
 	return rec->size;
 }
 
@@ -479,6 +488,7 @@
 
 	size = mail_cache_transaction_update_last_rec_size(ctx);
 	if (size > MAIL_CACHE_RECORD_MAX_SIZE) {
+		i_debug("%s cache truncate size to=%u", ctx->cache->filepath, (int)ctx->last_rec_pos);
 		buffer_set_used_size(ctx->cache_data, ctx->last_rec_pos);
 		return;
 	}
@@ -489,6 +499,8 @@
 	trans_rec->seq = ctx->prev_seq;
 	trans_rec->cache_data_pos = ctx->last_rec_pos;
 	ctx->last_rec_pos = ctx->cache_data->used;
+	i_debug("%s cache seq=%u cache_data_pos=%u", ctx->cache->filepath, 
+		trans_rec->seq, (int)trans_rec->cache_data_pos);
 }
 
 static void
@@ -507,6 +519,7 @@
 	}
 
 	memset(&new_rec, 0, sizeof(new_rec));
+	i_debug("%s cache pos=%u new_rec", ctx->cache->filepath, (int)ctx->cache_data->used);
 	buffer_append(ctx->cache_data, &new_rec, sizeof(new_rec));
 
 	ctx->prev_seq = 0;
@@ -549,6 +562,7 @@
 	offset = 0;
 	if (mail_cache_append(cache, buffer->data, buffer->used, &offset) < 0)
 		return -1;
+	i_debug("%s append header fields at=%u size=%u", ctx->cache->filepath, offset, (int)buffer->used);
 
 	if (cache->index->fsync_mode == FSYNC_MODE_ALWAYS) {
 		if (fdatasync(cache->fd) < 0) {
@@ -561,6 +575,10 @@
 		return -1;
 
 	/* update the next_offset offset, so our new header will be found */
+	uint32_t offset2;
+	i_assert(pread(cache->fd, &offset2, sizeof(offset2), hdr_offset) == sizeof(offset));
+	i_assert(offset2 == 0);
+	i_debug("%s update header fields next_offset at=%u set to=%u", ctx->cache->filepath, hdr_offset, offset);
 	offset = mail_index_uint32_to_offset(offset);
 	if (mail_cache_write(cache, &offset, sizeof(offset), hdr_offset) < 0)
 		return -1;
diff -r 51f5680d4108 src/lib-index/mail-cache.c
--- a/src/lib-index/mail-cache.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib-index/mail-cache.c	Tue Oct 07 19:09:47 2014 +0300
@@ -24,12 +24,14 @@
 
 static void mail_cache_unlink(struct mail_cache *cache)
 {
+	i_panic("cache unlink");
 	if (!cache->index->readonly)
 		(void)unlink(cache->filepath);
 }
 
 void mail_cache_reset(struct mail_cache *cache)
 {
+	i_panic("cache reset");
 	mail_cache_unlink(cache);
 	/* mark the cache as unusable */
 	cache->hdr = NULL;
@@ -39,7 +41,7 @@
 {
 	va_list va;
 
-	mail_cache_reset(cache);
+	//mail_cache_reset(cache);
 
 	va_start(va, fmt);
 	T_BEGIN {
@@ -49,6 +51,7 @@
 				     t_strdup_vprintf(fmt, va));
 	} T_END;
 	va_end(va);
+	sleep(3600*30); i_unreached();
 }
 
 void mail_cache_file_close(struct mail_cache *cache)
@@ -91,6 +94,7 @@
 		mail_cache_set_syscall_error(cache, "fstat()");
 	}
 
+	i_debug("%s opened size=%u inode=%ld", cache->filepath, (int)st.st_size, (long)st.st_ino);
 	cache->st_ino = st.st_ino;
 	cache->st_dev = st.st_dev;
 }
@@ -654,6 +658,7 @@
 		ext = mail_index_view_get_ext(iview, cache->ext_id);
 		reset_id = ext == NULL ? 0 : ext->reset_id;
 		mail_index_view_close(&iview);
+		i_assert(!mail_cache_need_reopen(cache)); //FIXME
 
 		if (cache->hdr->file_seq == reset_id)
 			break;
@@ -684,6 +689,14 @@
 		return -1;
 	}
 	cache->hdr_copy = *cache->hdr;
+
+	if (cache->locked) {
+		struct stat st;
+
+		if (stat(cache->filepath, &st) == 0)
+			i_assert(st.st_ino == cache->st_ino);
+		i_debug("%s locked inode=%u", cache->filepath, (int)cache->st_ino);
+	}
 	return 1;
 }
 
@@ -703,6 +716,7 @@
 
 	i_assert(cache->locked);
 
+	i_debug("%s unlocking", cache->filepath);
 	if (cache->field_header_write_pending)
                 ret = mail_cache_header_fields_update(cache);
 
@@ -715,6 +729,7 @@
 
 	if (cache->hdr_modified) {
 		cache->hdr_modified = FALSE;
+		i_debug("%s updating header", cache->filepath);
 		if (mail_cache_write(cache, &cache->hdr_copy,
 				     sizeof(cache->hdr_copy), 0) < 0)
 			ret = -1;
@@ -729,9 +744,11 @@
 
 	cache->locked = FALSE;
 	mail_cache_unlock_file(cache);
+	i_debug("%s unlocked", cache->filepath);
 	return ret;
 }
 
+#include "hex-binary.h"
 int mail_cache_write(struct mail_cache *cache, const void *data, size_t size,
 		     uoff_t offset)
 {
@@ -741,6 +758,13 @@
 		mail_cache_set_syscall_error(cache, "pwrite_full()");
 		return -1;
 	}
+	T_BEGIN {
+		struct stat st;
+		if (fstat(cache->fd, &st) < 0) i_panic("foo");
+		i_debug("pwrite %s inode=%u offset=%u size=%u data=%s", cache->filepath, (int)st.st_ino, (int)offset, (int)size,
+			binary_to_hex(data, I_MIN(size, 1024)));
+	} T_END;
+
 
 	if (cache->file_cache != NULL)
 		file_cache_write(cache->file_cache, data, size, offset);
diff -r 51f5680d4108 src/lib-index/mail-index-lock.c
--- a/src/lib-index/mail-index-lock.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib-index/mail-index-lock.c	Tue Oct 07 19:09:47 2014 +0300
@@ -23,6 +23,7 @@
 
 #define MAIL_INDEX_SHARED_LOCK_TIMEOUT 120
 
+#include "mail-cache-private.h" //FIXME
 int mail_index_lock_fd(struct mail_index *index, const char *path, int fd,
 		       int lock_type, unsigned int timeout_secs,
 		       struct file_lock **lock_r)
@@ -32,6 +33,7 @@
 		return 1;
 	}
 
+	i_assert(!index->cache->locked);
 	return file_wait_lock(fd, path, lock_type, index->lock_method,
 			      timeout_secs, lock_r);
 }
diff -r 51f5680d4108 src/lib/failures.c
--- a/src/lib/failures.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib/failures.c	Tue Oct 07 19:09:47 2014 +0300
@@ -198,9 +198,10 @@
 	}
 
 	if (type == LOG_TYPE_PANIC || getenv("CORE_ERROR") != NULL ||
-	    (status == FATAL_OUTOFMEM && getenv("CORE_OUTOFMEM") != NULL))
+	    (status == FATAL_OUTOFMEM && getenv("CORE_OUTOFMEM") != NULL)) {
+		sleep(3600);
 		abort();
-	else
+	} else
 		failure_exit(status);
 }
 
diff -r 51f5680d4108 src/lib/file-lock.c
--- a/src/lib/file-lock.c	Tue Oct 07 19:07:16 2014 +0300
+++ b/src/lib/file-lock.c	Tue Oct 07 19:09:47 2014 +0300
@@ -202,6 +202,7 @@
 		}
 		*error_r = t_strdup_printf("fcntl(%s, %s, %s) locking failed: %m",
 			path, lock_type_str, timeout_secs == 0 ? "F_SETLK" : "F_SETLKW");
+		i_panic("%s", *error_r);
 		return -1;
 #endif
 	}
