[plt-scheme] Cleanup on Servlet Timeout (Again)

From: Henk Boom (lunarc.lists at gmail.com)
Date: Thu Aug 28 00:09:36 EDT 2008

I've wrapped my head around the code you have supplied, basically by
re-writing the code in a style I am more comfortable with, and
customizing it a bit for use with sqlite (I've attached my version).
My biggest change was splitting 'serve' into smaller functions to make
its overall behaviour clearer. I have a couple of issues though.

First, the server will still be killed if the last process using it is
terminated. This means that the sqlite transaction may be held open
indefinitely, and the next server instantiated will not be able to
access the database. I can see one easy way to fix this, which would
be to call 'start-server' from the thread I use to start the web
server initially, effectively anchoring the server thread. Is there a
better way?

A separate issue is cleaning up sqlite resources allocated during
reading/writing. I use prepared statements as a simple means to guard
against SQL injection attacks, as they separate code from data in an
SQL query. For example, to do a select query I actually have the

(define (prepared-select query . params)
  (let ((statement (prepare db query)))
      (lambda ()
        (apply load-params statement params)
        (step* statement))
      (lambda () (finalize statement)))))

The kicker is that if the statement is not finalized, the sqlite
transaction will fail to close, so if a thread is terminated at the
wrong time I could lose access to my database. I could let the server
handle this whole process as well, but that doesn't sound like the
right solution to the problem. Is there a way to make this function
atomic, without manually putting it in a separate .c file and using
the ffi?

Thanks a lot for the help so far,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: monitor.ss
Type: application/octet-stream
Size: 7709 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20080828/521c1d2e/attachment.obj>

Posted on the users mailing list.