| From | Sent On | Attachments |
|---|---|---|
| Aleksandar Lazic | Apr 20, 2008 2:34 pm | |
| Bedros Hanounik | Apr 20, 2008 3:21 pm | |
| Manlio Perillo | Apr 20, 2008 3:26 pm | |
| Cliff Wells | Apr 20, 2008 3:34 pm | |
| Cliff Wells | Apr 20, 2008 3:38 pm | |
| Aleksandar Lazic | Apr 21, 2008 12:46 am | |
| Aleksandar Lazic | Apr 21, 2008 12:50 am | |
| Aleksandar Lazic | Apr 21, 2008 1:27 am | |
| Aleksandar Lazic | Apr 21, 2008 1:29 am | |
| Kiril Angov | Apr 21, 2008 7:47 pm | |
| Kiril Angov | Apr 21, 2008 7:48 pm | |
| Kiril Angov | Apr 21, 2008 7:57 pm | |
| Igor Sysoev | Apr 21, 2008 11:13 pm | |
| Marcin Kasperski | Apr 22, 2008 1:18 am | |
| Manlio Perillo | Apr 22, 2008 3:23 am | |
| Igor Sysoev | Apr 22, 2008 3:38 am | |
| Manlio Perillo | Apr 22, 2008 3:54 am | |
| Manlio Perillo | Apr 22, 2008 4:07 am | |
| Igor Sysoev | Apr 22, 2008 4:18 am | |
| Marcin Kasperski | Apr 22, 2008 5:05 am | |
| Manlio Perillo | Apr 22, 2008 6:18 am | |
| Manlio Perillo | Apr 22, 2008 6:50 am | |
| Igor Sysoev | Apr 22, 2008 7:10 am | |
| Manlio Perillo | Apr 22, 2008 7:30 am | |
| Kiril Angov | Apr 22, 2008 7:42 am | |
| Cliff Wells | Apr 22, 2008 12:24 pm | |
| Manlio Perillo | Apr 22, 2008 1:25 pm | |
| Manlio Perillo | Apr 22, 2008 1:27 pm | |
| Francisco Valladolid | Apr 22, 2008 2:12 pm | |
| Cliff Wells | Apr 22, 2008 2:43 pm | |
| Sean Allen | Apr 22, 2008 3:10 pm | |
| Jay Reitz | Apr 22, 2008 10:13 pm | |
| Aleksandar Lazic | Apr 22, 2008 11:06 pm | |
| Aleksandar Lazic | Apr 22, 2008 11:09 pm | |
| Aleksandar Lazic | Apr 22, 2008 11:18 pm | |
| Mike Crawford | Apr 25, 2008 2:02 pm | |
| Igor Sysoev | Apr 25, 2008 10:46 pm | |
| Aleksandar Lazic | Apr 25, 2008 10:56 pm | |
| Igor Sysoev | Apr 25, 2008 11:53 pm | |
| Manlio Perillo | Apr 26, 2008 2:59 am | |
| Igor Sysoev | Apr 26, 2008 3:22 am | |
| Manlio Perillo | Apr 26, 2008 3:45 am | |
| Manlio Perillo | Apr 26, 2008 4:24 am | |
| Manlio Perillo | Apr 26, 2008 7:37 am | .c |
| Manlio Perillo | Apr 26, 2008 8:28 am | |
| Igor Sysoev | Apr 26, 2008 8:57 am | |
| Adrian Perez | Apr 26, 2008 9:52 am | |
| Manlio Perillo | Apr 26, 2008 10:42 am | |
| Mike Crawford | Apr 28, 2008 8:05 am | |
| Adrian Perez | May 4, 2008 10:40 am |
| Subject: | Re: OT: 'best' dynamic language | |
|---|---|---|
| From: | Manlio Perillo (manl...@public.gmane.org) | |
| Date: | Apr 26, 2008 7:37:29 am | |
| List: | ru.sysoev.nginx | |
| Attachments: | ||
Manlio Perillo ha scritto:
Igor Sysoev ha scritto:
[...] The problem is that interpreter MUST TEST EVERY operation result that may fail on memory allocation. And it MUST return an error to all higher levels, closing and freeing all allocated resources on the back way.
The existent interpreters either do not test result in most cases (perl), or simply exit(), or in best case they throw exception. Exceptions are easy way to program (you not need to test most operations) and cheap way to test results (for the same reason), but they may lead to socket/file descriptor/etc leak.
I understand the problem, however I think that Lua is still usable.
I'm reading the source code of Lua io library, and any opened file is closed when reached by the gc.
This means that when Nginx detects an error, it can just force a full gc cycle.
If this still does not sounds safe, Nginx can just create a Lua state (interpreter) for each request, finalizing it when the request is finalized.
This is both feasible and efficient (but better is one of Lua language developer can confirm it).
I have written a small program and I can confirm that opened files are correctly closed when the Lua interpreter is finalized.
The program is attached: the Lua code simply open a big file and read all its content in memory.
There is a custom memory allocator that make sure to fail when a large amount of memory is required.
There is a strange behaviour, however. The status code returned by luaL_dostring should be 4 (LUA_ERRMEM), instead 1 is returned.
Regards Manlio Perillo
/* * Test program for checking that all opened files are closed in case of * exception (especially for memory allocation exceptions). * * Author: Manlio Perillo */
#include <stdio.h> #include <stdlib.h>
#include <lua.h> #include <lauxlib.h>
#define MAX_ALLOC 10240
#define CHUNCK_1 "f = io.open(\"/usr/local/nginx/buf\");" #define CHUNCK_2 "s = f:read(\"*a\");"
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { printf("l_alloc: %d:%d\n", osize, nsize);
if (nsize == 0) { free(ptr); return NULL; } else if (nsize > MAX_ALLOC) { printf("l_alloc: out of memory\n"); return NULL; } else { return realloc(ptr, nsize); } }
int main (int argc, char **argv) { int status; lua_State *L = lua_newstate(l_alloc, NULL);
if (L == NULL) { printf("cannot create state: not enough memory"); return EXIT_FAILURE; }
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ luaL_openlibs(L); /* open standard libraries */ lua_gc(L, LUA_GCRESTART, 0);
/* * Open a big file (> 100 KB) and read in memory its entire content. * Check if the file is closed with: * lsof +d /usr/local/nginx */
printf("press a key to start execution"); getchar();
status = luaL_dostring(L, CHUNCK_1); printf("execution status 1: %i\n", status);
printf("press a key to continue execution"); getchar();
status = luaL_dostring(L, CHUNCK_2); printf("execution status 2: %i\n", status);
printf("press a key to close Lua state"); getchar();
lua_close(L);
printf("press a key to exit"); getchar();
return EXIT_SUCCESS; }






.c