common-lisphunchentoot

Error: Unbound variable: *AJAX-PROCESSOR* using HT-SIMPLE-AJAX


I'm using HT-SIMPLE-AJAX to provide a simple JSON structure over AJAX. It works beautifully if the function defined by defun-ajaxis compiled after the lisp image and the server is started.

If I load the lisp program (with ccl --load) with the function defined, I get this error:

Error: Unbound variable: *AJAX-PROCESSOR* While executing: #, in process listener(1).

Type :GO to continue, :POP to abort, :R for a list of available restarts. If continued: Skip loading "/home/hunchentoot/quicklisp/local-projects/gac-man/run.lisp" Type :? for other options.

The function is as follows:

(defun-ajax machine-info (serial) (*ajax-processor*)
  (let* ((serialn (remove #\" serial)))
    (concatenate 'string
     "Lots of boring stuff" "here")))

The ajax processor is created in another function, called at the start of the program:

(defun start ()
  (setup)
  (connect-to-database)
  (defvar *web-server* (start (make-instance 'hunchentoot:easy-acceptor :port 8080
                     :document-root #p"~/www/")))
  (defvar *ajax-processor* 
  (make-instance 'ajax-processor :server-uri "/ajax"))
  (print "Starting web server...")
  (setf *show-lisp-errors-p* t
    *show-lisp-backtraces-p* t)
  (define-easy-handler (docroot :uri "/") () (docroot)
  ....
  ....
   (setq *dispatch-table* (list 'dispatch-easy-handlers 
               (create-ajax-dispatcher *ajax-processor*)))))

And yet if I start everything and then compile in the function through slime later, it works just fine. Why is this error occurring?

I'm using Clozure Common Lisp on 64-bit Linux.


Solution

  • It seems that your defun-ajax form is loaded before the start function has run. That is not surprising. Usually, all code is loaded, and only then the entry point is called.

    You should always be very suspicious of defvar, defun, defparameter etc. forms appearing in a function body. They don't belong there. Put them as toplevel forms, so they are loaded as part of the program. Most of the things defined during the run of the start function shown should really be toplevel forms.