From AOLserver Wiki
Revision as of 19:03, 7 February 2009 by Gustafn (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


ns_proxy - Execute Tcl scripts in an external process (aolserver module)


ns_proxy active pool
ns_proxy cleanup
ns_proxy config pool ?-opt val -opt val ...
ns_proxy eval handle script ?timeout?
ns_proxy get pool ?-handle n -timeout ms?
ns_proxy ping handle
ns_proxy release handle
ns_proxy recv handle
ns_proxy send handle script
ns_proxy wait handle ?timeout?


This command provides a simple, robust proxy mechanism to evaluate Tcl scripts in a separate, pipe-connected process. This approach can be useful both to isolate potentially thread-unsafe code outside the address space of a aolserver or to enable separation and timeout of potentially misbehaving, long running scripts. The proxy includes core Tcl commands as well as AOLserver commands from the libnsd library. Evaluating scripts in a proxy process can be used to isolate and/or timeout thread-unsafe or otherwise unsafe 3rd party code.
The legal options are:
  • ns_proxy active pool
Returns a list of all currently evaluating scripts in proxies for the given pool. The output is a list which includes two elements, the string name of the proxy handle and the string for the script being executed. It is also possible to view the currently evaluating scripts with the Unix ps command as the proxy slave process re-writes it's command argument space with the request script before evaluation and clears it after sending the result.
  • ns_proxy cleanup
Releases any handles from any pools currently owned by a thread. This command is intended to be used as part of a garbage collection step at the end of a transaction. Calling this command within AOLserver is not necessary as the module registers a trace to release all handles via the Ns_TclRegisterTrace facility when interpreters are deallocated after a transaction, for example, at the end of a connection.
  • ns_proxy config pool ? -init script? ? -reinit script? ? -min value? ? -max value? ? -exec program? ? -gettimeout value? ? -evaltimeout value? ? -sendtimeout value? ? -recvtimeout value? ? -waittimeout value?
Configure the specified pool or obtain current configuration values. When ns_proxy config is called without options, a list of the current options in the form -opt val ... is returned. init can be used to specify a script to be evaluated, when the proxy (slave) is started. This can be used to load additional libraries and/or source script files. The default is no script. reinit Specifies a script to evaluate after being allocated and before being returned to the caller. This can be used to re-initalizes shared state. The default is no script. min and max are the minimum and maximum number of slaves (min defaults to 0, max 0 means to disable this pool). exec names the program (filename) to be executed. This defaults to nsproxy in the bin subdirectory of the AOLserver process. It is possible to create a custom program and enter the proxy event loop with the Ns_ProxyMain application startup routine; see the source code for details. gettimeout specifies the maximum time in ms to wait to allocate handles from the pool (default 500). evaltimeout specifies the maximum time in ms to wait for a script to be evaluated in a proxy (default 100). This parameter can be overridden on a per-call basis with the optional timeout parameter to ns_proxy eval. The default is 0 milliseconds i.e. infinite. sendtimeout and recvtimeout specify the maximum time to wait to send a script and receive a result from a proxy (default 100 milliseconds). waittimeout specifies the maximum time to wait for a proxy to exit. The wait is performed in a dedicated reaper thread. The reaper will close the connection pipe and wait the given timeout. If the timeout is exceeded, the reaper will send a SIGTERM signal and finally a SIGKILL signal to ensure the process eventually exits. The default is 100 milliseconds which should be ample time for a graceful exit unless the process is hung executing a very long, misbehaving script, resulting in a more disruptive SIGTERM or SIGKILL.
  • ns_proxy eval handle script ?timeout?
Evalutes script in the proxy specified by handle. The optional timeout argument specifies a maximum number of milliseconds to wait for the command to complete before raising an error (see ERROR HANDLING below for details on handling errors).
  • ns_proxy get pool ? -handles value? ? -timeout value?
Returns one or more handles to proxies from the specified pool. The pool will be created with default options if it does not already exist. The option -handle can be used to specify the number of handles allocated (default: 1). The option timeout specifies the maximum amount of time in milliseconds to wait for the handles to become available before raising an error. Requesting more than one handle in a single call (if more than one handle is required) is necessary as it is an error to request handles from a pool from which handles are already owned by the thread. This restriction is implemented to avoid possible deadlock conditions. The handle returned by this command can be used as a scalar value for other ns_proxy commands, or it can be used as Tcl command itself (see ns_proxy eval for more information).
  • ns_proxy ping handle
Check if handle is alive by sending an empty request. This command is not normally required as the ns_proxy eval command will also verify and restart proxies as needed.
  • ns_proxy recv
Receives a response from a script that was sent via ns_proxy send and waited on via ns_proxy wait.
  • ns_proxy release handle
Release a single handle. All handles owned by a thread to the corresponding pool must be returned before any handles can be allocated again. Within AOLserver, a call to this routine is recommended for clarity but not stricting necessary. As described above, AOLserver installs a trace to release all handles at the end of every connection during interpreter deallocation.
  • ns_proxy send handle script
Sends script in the proxy specified by handle. Unlike with ns_proxy eval, this option will return immediately while the script continues to execute in the proxy process. A later ns_proxy wait followed by an ns_proxy recv is expected.
  • ns_proxy wait handle ?timeout?
Waits for a script sent via ns_proxy send in the proxy specified by the handle argument to complete. The optional timeout parameter specifies the number of milliseconds to wait for the script to complete, the default is an indefinite wait.


Errors generated by a script evaluated in a proxy interpreter are completely returned to the calling interpreter, including mapping the errorInfo and errorInfo global variables from the proxy to the parent if present and raising a Tcl exception. This enables proxy code to look very similar to that which may use the internal eval command.
Errors raised by a failure to communicate with the proxy process due to a timeout or unexpected process exit are also communicated back to the parent interpreter as Tcl exceptions. To distinguish between these cases, communication related errors set the errorCode global variable with the first element NSPROXY. The second element is one of the following:
The interpreter attempted to allocate handles from a pool from which it already owns one or more handles.
The slave program specified by the -exec program option could not be started.
Insufficient handles available in pool.
There was an error receiving the result from the slave process.
There was an error sending the script to the slave process.
The timeout specified for the pool by the -evaltimeout option or as the optional argument to the current call to ns_proxy eval was exceeded.


The following demonstrates sending a script to a remote proxy:

       set handle [ns_proxy get myproxy]
       ns_proxy eval $handle {info patchlevel}
       ns_proxy release $handle

The following demonstrates an asyncronous request:

       set handle [ns_proxy get myproxy]
       ns_proxy send $handle {long running script}
       ... continue other work ...
       ns_proxy wait $handle
       set result [ns_proxy recv $handle]
       ns_proxy release $handle

The following demonstrates using multiple proxies:

       ns_proxy config myproxy -max 10
       set handles [ns_proxy get myproxy -handle 10]
       foreach h $handles {
               ns_proxy eval $h {puts "alive: [pid]"}
       ns_proxy cleanup


ns_eval -