Bug 181052 - [patch] make lang/lua not hang on thread creation
Summary: [patch] make lang/lua not hang on thread creation
Status: Closed Overcome By Events
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: Normal Affects Only Me
Assignee: Sergey A. Osokin
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-08-05 14:50 UTC by Vitaly Magerya
Modified: 2018-02-15 20:25 UTC (History)
4 users (show)

See Also:
mva: maintainer-feedback? (lua)


Attachments
file.diff (1.16 KB, patch)
2013-08-05 14:50 UTC, Vitaly Magerya
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vitaly Magerya 2013-08-05 14:50:00 UTC
Currently lang/lua links the 'lua' binary without -pthread flag;
this results in any call to 'pthread_create' to completely hang
the program.

Normally this wouldn't be an issue, since Lua doesn't use any
threading functions, but the problem propagates to all the C
extensions as well. For example, if you'll install devel/lgi
(the GObject/Gtk bindings), and try this simple program:

    $ lua-5.1
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > lgi = require 'lgi'
    > gtk = lgi.Gtk
    > gtk.FileChooserDialog {}

.. it will hang at this point. The reason being that Gtk uses
threads internally and tries to spawn a few when you create a
FileChooser object.

That is to say, this problem makes lang/lgi basically unusable,
and prevents any kind of Lua Gtk programs working on FreeBSD.

More directly, you can create an extension that calls
'pthread_create':

    $ cat >test.c <<'EOF'
    #include <lauxlib.h>
    #include <lua.h>
    #include <pthread.h>
    #include <stdio.h>

    static void *
    thread_func(void *arg)
    {
        pthread_exit(0);
    }

    static int
    test(lua_State *L)
    {
        pthread_t thread;
        printf("PTHREAD CREATE\n");
        pthread_create(&thread, NULL, thread_func, NULL);
        printf("PTHREAD JOIN\n");
        pthread_join(thread, NULL);
        printf("DONE\n");
    }

    static const struct luaL_Reg lib[] = {
        {"test", test},
        {NULL, NULL}
    };

    LUALIB_API int
    luaopen_test(lua_State *L)
    {
    #if LUA_VERSION_NUM < 502
        luaL_register(L, "test", lib);
    #else
        luaL_newlib(L, lib);
    #endif
        return 1;
    }
    EOF

    $ cc -I/usr/local/include/lua51 -shared -pthread -fPIC -o test.so test.c

    $ lua-5.1
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > test = require "test"
    > test.test()
    PTHREAD CREATE

.. and again, it hangs.

Fix: See the attached patch. With it both 'lua' and 'lua.so' link
with -pthread.

Note that lang/lua52 and lang/luajit are affected too, but this
PR only deals with lang/lua.

Patch attached with submission follows:
Comment 1 Edwin Groothuis freebsd_committer freebsd_triage 2013-08-05 14:50:09 UTC
Responsible Changed
From-To: freebsd-ports-bugs->lua

Over to maintainer (via the GNATS Auto Assign Tool)
Comment 2 dfilter service freebsd_committer freebsd_triage 2013-08-05 21:37:43 UTC
Author: mandree
Date: Mon Aug  5 20:37:35 2013
New Revision: 324286
URL: http://svnweb.freebsd.org/changeset/ports/324286

Log:
  Link lua .so + program with pthread to prevent hangs in extensions that
  use pthreads.
  
  Bump PORTREVISION to pull this in.
  
  PR:		ports/181052
  Submitted by:	Vitaly Magerya <vmagerya@gmail.com>
  Approved by:	mandree@ on behalf of lua@

Modified:
  head/lang/lua/Makefile
  head/lang/lua/files/patch-src-Makefile
  head/lang/lua52/Makefile

Modified: head/lang/lua/Makefile
==============================================================================
--- head/lang/lua/Makefile	Mon Aug  5 20:21:58 2013	(r324285)
+++ head/lang/lua/Makefile	Mon Aug  5 20:37:35 2013	(r324286)
@@ -3,7 +3,7 @@
 
 PORTNAME=	lua
 PORTVERSION=	5.1.5
-PORTREVISION=	5
+PORTREVISION=	6
 CATEGORIES=	lang
 MASTER_SITES=	http://www.lua.org/ftp/ \
 		ftp://ftp.ntua.gr/pub/lang/lua/ \
@@ -33,7 +33,7 @@ MAKE_ENV=	LUA_SONAME="liblua-${LUA_VER}.
 MAKE_ARGS=	__MAKE_CONF=${NONEXISTENT}
 # liblua.so requires libm, so make sure it has an explicit dependency
 # so that applications need not second-guess lua's dependencies.
-LDFLAGS+=	-lm
+LDFLAGS+=	-lm -pthread
 
 MAN1=		lua-${LUA_VER}.1 luac-${LUA_VER}.1
 DOCSDIR=	${PREFIX}/share/doc/${LUA_SUBDIR}

Modified: head/lang/lua/files/patch-src-Makefile
==============================================================================
--- head/lang/lua/files/patch-src-Makefile	Mon Aug  5 20:21:58 2013	(r324285)
+++ head/lang/lua/files/patch-src-Makefile	Mon Aug  5 20:37:35 2013	(r324286)
@@ -1,6 +1,6 @@
---- Makefile.orig	2008-01-19 17:37:58.000000000 -0200
-+++ Makefile	2008-08-10 16:00:41.000000000 -0300
-@@ -9,7 +9,8 @@
+--- Makefile.orig	2012-02-13 22:41:22.000000000 +0200
++++ Makefile	2013-08-05 15:56:32.000000000 +0300
+@@ -9,10 +9,11 @@
  
  CC= gcc
  CFLAGS= -O2 -Wall $(MYCFLAGS)
@@ -9,7 +9,11 @@
 +AR= ar
  RANLIB= ranlib
  RM= rm -f
- LIBS= -lm $(MYLIBS)
+-LIBS= -lm $(MYLIBS)
++LIBS= -lm -pthread $(MYLIBS)
+ 
+ MYCFLAGS=
+ MYLDFLAGS=
 @@ -31,12 +32,13 @@
  
  LUA_T=	lua

Modified: head/lang/lua52/Makefile
==============================================================================
--- head/lang/lua52/Makefile	Mon Aug  5 20:21:58 2013	(r324285)
+++ head/lang/lua52/Makefile	Mon Aug  5 20:37:35 2013	(r324286)
@@ -3,6 +3,7 @@
 
 PORTNAME=	lua
 PORTVERSION=	5.2.2
+PORTREVISION=	1
 CATEGORIES=	lang
 MASTER_SITES=	http://www.lua.org/ftp/
 
@@ -30,7 +31,7 @@ MAKE_ENV=	LUA_SONAME="liblua-${LUA_VER}.
 		MYCFLAGS="${CFLAGS}" MYLDFLAGS="${LDFLAGS}"
 # liblua.so requires libm, so make sure it has an explicit dependency
 # so that applications need not second-guess lua's dependencies.
-LDFLAGS+=	-lm
+LDFLAGS+=	-lm -pthread
 
 MAN1=		lua-${LUA_VER}.1 luac-${LUA_VER}.1
 DOCSDIR=	${PREFIX}/share/doc/${LUA_SUBDIR}
_______________________________________________
svn-ports-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-ports-all
To unsubscribe, send any mail to "svn-ports-all-unsubscribe@freebsd.org"
Comment 3 Matthias Andree freebsd_committer freebsd_triage 2013-08-05 21:37:50 UTC
Responsible Changed
From-To: lua->osa

Vitaly, thanks for submitting this excellent problem report and patch. 
I have committed the change for lang/lua (5.1) and lua52. 
Sergey, please see if this is required for luajit, and after that, pass it on 
to the lua50 maintainer. Thank you.
Comment 4 Carlo Strub freebsd_committer freebsd_triage 2014-09-14 10:39:56 UTC
Is this PR still relevant?
Comment 5 Vitaly Magerya 2014-09-14 20:27:46 UTC
(In reply to Carlo Strub from comment #4)
> Is this PR still relevant?

Yes. While lang/lua51 and lang/lua52 are fixed, lang/luajit is
still broken as described.
Comment 6 Carlo Strub freebsd_committer freebsd_triage 2014-09-15 05:01:10 UTC
Maybe Matthias has an idea.
Comment 7 Matthias Andree freebsd_committer freebsd_triage 2014-09-15 07:30:39 UTC
Sorry, no idea - and I'm for now not using lua very much, and as a consequence, I quit the lua@ team months ago.
Comment 8 Carlo Strub freebsd_committer freebsd_triage 2014-09-15 11:21:15 UTC
Back to pool
Comment 9 Marcus von Appen freebsd_committer freebsd_triage 2014-09-17 17:02:37 UTC
Over to maintainer
Comment 10 Baptiste Daroussin freebsd_committer freebsd_triage 2014-12-29 23:17:27 UTC
Over to osa lua ports are fixed while I don't know for luajit, osa being the maintainer of luajit I'll let him decide.

Please also not that a work is being done on FreeBSD current (not yet committed at that time which will make this PR irrelevant 

Reference: https://lists.freebsd.org/pipermail/freebsd-arch/2014-December/016550.html
Comment 11 Walter Schwarzenfeld freebsd_triage 2018-01-12 04:12:42 UTC
Any advance here? Please, maintainer feedback!
Comment 12 Walter Schwarzenfeld freebsd_triage 2018-02-15 19:00:27 UTC
port lua does not exist anymore. lua51, lua52 and lua53 shows all 
LDFLAGS+=       -lm -pthread
The Makefile are complete reworked. 
It is overcome by events.
Comment 13 Vitaly Magerya 2018-02-15 20:21:46 UTC
The last remaining problem is in lang/luajit.
Comment 14 Walter Schwarzenfeld freebsd_triage 2018-02-15 20:25:35 UTC
see Bug #191476.