Project

General

Profile

bug760.patch

Patch to work around problem of crashing on large files -- geoff - Anonymous, 2007-06-26 22:39

View differences:

chunk.c 2007-05-24 18:13:08.000000000 -0600
325 325
	return len;
326 326
}
327 327

  
328
off_t chunkqueue_memory_used(chunkqueue *cq) {
329
	off_t used = 0;
330
	chunk *c;
331
	
332
	for (c = cq->first; c; c = c->next) {
333
		used += sizeof *c;
334
		switch (c->type) {
335
		case MEM_CHUNK:
336
			used += c->mem->used;
337
			break;
338
		case FILE_CHUNK:
339
			break;
340
		default:
341
			break;
342
		}
343
	}
344
	
345
	return used;
346
}
347

  
328 348
off_t chunkqueue_written(chunkqueue *cq) {
329 349
	off_t len = 0;
330 350
	chunk *c;
chunk.h 2007-05-24 18:13:08.000000000 -0600
60 60
int chunkqueue_remove_finished_chunks(chunkqueue *cq);
61 61

  
62 62
off_t chunkqueue_length(chunkqueue *c);
63
off_t chunkqueue_memory_used(chunkqueue *c);
63 64
off_t chunkqueue_written(chunkqueue *c);
64 65
void chunkqueue_free(chunkqueue *c);
65 66
void chunkqueue_reset(chunkqueue *c);
mod_fastcgi.c 2007-05-24 18:19:13.000000000 -0600
326 326
	FCGI_STATE_CONNECT_DELAYED, 
327 327
	FCGI_STATE_PREPARE_WRITE, 
328 328
	FCGI_STATE_WRITE, 
329
	FCGI_STATE_WRITE_QUEUE_FULL,
329 330
	FCGI_STATE_READ 
330 331
} fcgi_connection_state_t;
331 332

  
333
/*
334
 * Limit on how large any write queue is allowed to grow.  If the
335
 * queue becomes larger than this, lighttpd will temporarily remove
336
 * the *input* side fd from the event handler until the queue length
337
 * drops.  Because of the hacky way in which this feature is
338
 * implemented, the reduced queue length won't be noticed for up to
339
 * one second.  For that reason, the queue length should be large
340
 * enough to sustain 2-3 seconds of output across the server's
341
 * connection.  On a 100 Mbps Ethernet, that works out to about 25 MB.
342
 * Note that a machine that serves many connections might still run
343
 * out of memory (for example, if MAX_WRITE_QUEUE_SIZE is 32 MB, 100
344
 * connections will expect the server to allocate 3.2 GB, which most
345
 * systems can't handle).
346
 *
347
 * The correct way to implement this feature is to have
348
 * MAX_WRITE_QUEUE_SIZE fairly small, probably around 1 MB, but have
349
 * the wakeup done by chunk.c when the chunk queue drains
350
 * appropriately.  That requires more extensive changes, and I'm
351
 * trying to limit the amount of code I change, so I'll leave that
352
 * improvement to someone more familiar with lighttpd.  Geoff
353
 * Kuenning, 5/24/2007.
354
 */
355
#define MAX_WRITE_QUEUE_SIZE (1 << 25)	/* 32 MB */
356

  
332 357
typedef struct {
333 358
	fcgi_proc *proc;
334 359
	fcgi_extension_host *host;
......
2395 2420
	fcgi_extension_host *host= hctx->host;
2396 2421
	fcgi_proc *proc   = hctx->proc;
2397 2422
	
2423
	/*
2424
	 * Before reading, find out if the write queue is overfull.
2425
	 * If so, suspend reading.  Geoff Kuenning, 5/24/2007.
2426
	 */
2427
	if (chunkqueue_memory_used(con->write_queue) >= MAX_WRITE_QUEUE_SIZE) {
2428
		fdevent_event_del(srv->ev, &(hctx->fde_ndx), fcgi_fd);
2429
		fcgi_set_state(srv, hctx, FCGI_STATE_WRITE_QUEUE_FULL);
2430
		return 0;
2431
	}
2432

  
2398 2433
	/* 
2399 2434
	 * check how much we have to read 
2400 2435
	 */
......
2983 3018
	case FCGI_STATE_READ:
2984 3019
		/* waiting for a response */
2985 3020
		break;
3021
	case FCGI_STATE_WRITE_QUEUE_FULL:
3022
		/*
3023
		 * We stopped reading because the write queue is full.
3024
		 * See if we can read again.  If so, put the fd back
3025
		 * in the event list.  Geoff Kuenning, 5/24/2007.
3026
		 */
3027
		if (chunkqueue_memory_used(con->write_queue) < MAX_WRITE_QUEUE_SIZE) {
3028
			fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
3029
			fcgi_set_state(srv, hctx, FCGI_STATE_READ);
3030
		
3031
		}
3032
		break;
2986 3033
	default:
2987 3034
		log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
2988 3035
		return HANDLER_ERROR;
......
3580 3627
			fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
3581 3628
			
3582 3629
			break;
3630
		case FCGI_STATE_WRITE_QUEUE_FULL:
3631
			/*
3632
			 * We stopped reading because the write queue
3633
			 * is full.  See if we can read again.  If so,
3634
			 * put the fd back in the event list.  Geoff
3635
			 * Kuenning, 5/24/2007.
3636
			 */
3637
			if (chunkqueue_memory_used(con->write_queue) < MAX_WRITE_QUEUE_SIZE) {
3638
				fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
3639
				fcgi_set_state(srv, hctx, FCGI_STATE_READ);
3640

  
3641
			}
3642
			break;
3583 3643
		case FCGI_STATE_INIT:
3584 3644
			/* at reconnect */
3585 3645
			break;