Skip to content

Commit

Permalink
lua: close Lua file descriptors when connection is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Dec 4, 2023
1 parent d532f84 commit adcc025
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cm4all-qrelay (0.26) unstable; urgency=low

* lua: remove deprecated method "mail:get_cgroup()"
* lua: close Lua file descriptors when connection is closed

--

Expand Down
2 changes: 1 addition & 1 deletion libcommon
4 changes: 3 additions & 1 deletion src/Connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ QmqpRelayConnection::QmqpRelayConnection(size_t max_size,
peer_cred(GetSocket().GetPeerCredentials()),
handler(std::move(_handler)),
logger(parent_logger, MakeLoggerDomain(peer_cred, address).c_str()),
auto_close(handler->GetState()),
thread(handler->GetState()),
connect(event_loop, *this),
client(event_loop, 256, *this) {}
Expand Down Expand Up @@ -78,7 +79,8 @@ QmqpRelayConnection::OnRequest(AllocatedArray<std::byte> &&payload)

handler->Push(L);

NewLuaMail(L, GetMainState(), std::move(mail), peer_cred);
NewLuaMail(L, GetMainState(), auto_close,
std::move(mail), peer_cred);

Resume(L, 1);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "event/net/djb/NetstringServer.hxx"
#include "event/net/djb/NetstringClient.hxx"
#include "net/djb/NetstringGenerator.hxx"
#include "lua/AutoCloseList.hxx"
#include "lua/Ref.hxx"
#include "lua/Resume.hxx"
#include "lua/ValuePtr.hxx"
Expand All @@ -32,6 +33,8 @@ class QmqpRelayConnection final :
const Lua::ValuePtr handler;
ChildLogger logger;

Lua::AutoCloseList auto_close;

NetstringGenerator generator;
NetstringHeader sender_header;

Expand Down
13 changes: 11 additions & 2 deletions src/LMail.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "MutableMail.hxx"
#include "LAction.hxx"
#include "Action.hxx"
#include "lua/AutoCloseList.hxx"
#include "lua/Class.hxx"
#include "lua/Error.hxx"
#include "lua/FenvCache.hxx"
Expand All @@ -29,11 +30,15 @@ extern "C" {
#include <string.h>

class IncomingMail : public MutableMail {
Lua::AutoCloseList *auto_close;

const struct ucred cred;

public:
IncomingMail(lua_State *L, MutableMail &&src, const struct ucred &_peer_cred)
IncomingMail(lua_State *L, Lua::AutoCloseList &_auto_close,
MutableMail &&src, const struct ucred &_peer_cred)
:MutableMail(std::move(src)),
auto_close(&_auto_close),
cred(_peer_cred)
{
lua_newtable(L);
Expand Down Expand Up @@ -313,6 +318,9 @@ IncomingMail::Index(lua_State *L)
Lua::RaiseCurrent(L);
}

// auto-close the file descriptor when the connection is closed
auto_close->Add(L, Lua::RelativeStackIndex{-1});

// copy a reference to the fenv (our cache)
Lua::SetFenvCache(L, 1, name, Lua::RelativeStackIndex{-1});

Expand Down Expand Up @@ -352,9 +360,10 @@ RegisterLuaMail(lua_State *L)

MutableMail *
NewLuaMail(lua_State *L, lua_State *main_L,
Lua::AutoCloseList &auto_close,
MutableMail &&src, const struct ucred &peer_cred)
{
return LuaMail::New(L, main_L, std::move(src), peer_cred);
return LuaMail::New(L, main_L, auto_close, std::move(src), peer_cred);
}

MutableMail &
Expand Down
2 changes: 2 additions & 0 deletions src/LMail.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
struct ucred;
struct lua_State;
struct MutableMail;
namespace Lua { class AutoCloseList; }

void
RegisterLuaMail(lua_State *L);
Expand All @@ -19,6 +20,7 @@ RegisterLuaMail(lua_State *L);
*/
MutableMail *
NewLuaMail(lua_State *L, lua_State *main_L,
Lua::AutoCloseList &auto_close,
MutableMail &&src, const struct ucred &peer_cred);

MutableMail &
Expand Down

0 comments on commit adcc025

Please sign in to comment.