lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This might have been a false alarm. I forgot that I had a small extension that I wrote myself. When I removed this the segfault went away, oddly enough when there is less data to process it does not segfault either - so perhaps the problem is GC and my extension, a lot of data forces the GC to kick in to give the error. However the valgrind reports still report errors when the extension is removed:

With extension and segfault

ERROR SUMMARY: 303 errors from 275 contexts (suppressed: 155 from 21)

Without extension, no segfault

ERROR SUMMARY: 278 errors from 274 contexts (suppressed: 155 from 21)
For reference here is my extension

/*
 * gcc -Wall -shared -fPIC -o time_now.so -llua time_now.c
 *
 * This is an unusual case. I need the current time, down to microseconds,
 * as a string for things like logs. Not something that Lua is good at.
 */

#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"

#include <stdio.h>
#include <sys/time.h>

#define BUFFER_SIZE 64
#define LENGTH_OF_DATE_TIME 19
#define LENGTH_OF_MS 8

static int time_now (lua_State *L) {
  char buffer[BUFFER_SIZE];
  struct tm* tm_info;
  struct timeval tv;
  long int ms;

  gettimeofday(&tv, NULL);

  tm_info = localtime(&tv.tv_sec);

  strftime(buffer, BUFFER_SIZE, "%Y-%m-%dT%H:%M:%S", tm_info);

  ms = (long int)tv.tv_usec % 1000000;

  snprintf(buffer + LENGTH_OF_DATE_TIME, LENGTH_OF_MS, ".%06ld", ms);

  lua_pushstring(L, buffer);

  return 1;
}

int luaopen_time_now(lua_State *L){
  lua_register(L, "time_now", time_now);
  return 0;
}