Skip to content

Commit 6426798

Browse files
committed
Unref callback context when call unsetopt function.
1 parent 316cf79 commit 6426798

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

src/lceasy.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ static int lcurl_easy_unset_WRITEFUNCTION(lua_State *L){
571571
}
572572
curl_easy_setopt(p->curl, CURLOPT_WRITEDATA, NULL);
573573

574+
luaL_unref(L, LCURL_LUA_REGISTRY, p->wr.cb_ref);
575+
luaL_unref(L, LCURL_LUA_REGISTRY, p->wr.ud_ref);
576+
p->wr.cb_ref = p->wr.ud_ref = LUA_NOREF;
577+
574578
lua_settop(L, 1);
575579
return 1;
576580
}
@@ -584,6 +588,10 @@ static int lcurl_easy_unset_READFUNCTION(lua_State *L){
584588
}
585589
curl_easy_setopt(p->curl, CURLOPT_READDATA, NULL);
586590

591+
luaL_unref(L, LCURL_LUA_REGISTRY, p->rd.cb_ref);
592+
luaL_unref(L, LCURL_LUA_REGISTRY, p->rd.ud_ref);
593+
p->rd.cb_ref = p->rd.ud_ref = LUA_NOREF;
594+
587595
lua_settop(L, 1);
588596
return 1;
589597
}
@@ -597,6 +605,10 @@ static int lcurl_easy_unset_HEADERFUNCTION(lua_State *L){
597605
}
598606
curl_easy_setopt(p->curl, CURLOPT_HEADERDATA, NULL);
599607

608+
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.cb_ref);
609+
luaL_unref(L, LCURL_LUA_REGISTRY, p->hd.ud_ref);
610+
p->hd.cb_ref = p->hd.ud_ref = LUA_NOREF;
611+
600612
lua_settop(L, 1);
601613
return 1;
602614
}
@@ -615,6 +627,10 @@ static int lcurl_easy_unset_PROGRESSFUNCTION(lua_State *L){
615627
curl_easy_setopt(p->curl, CURLOPT_XFERINFODATA, NULL);
616628
#endif
617629

630+
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.cb_ref);
631+
luaL_unref(L, LCURL_LUA_REGISTRY, p->pr.ud_ref);
632+
p->pr.cb_ref = p->pr.ud_ref = LUA_NOREF;
633+
618634
lua_settop(L, 1);
619635
return 1;
620636
}
@@ -642,6 +658,10 @@ static int lcurl_easy_unset_SEEKFUNCTION(lua_State *L){
642658
}
643659
curl_easy_setopt(p->curl, CURLOPT_SEEKDATA, NULL);
644660

661+
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.cb_ref);
662+
luaL_unref(L, LCURL_LUA_REGISTRY, p->seek.ud_ref);
663+
p->seek.cb_ref = p->seek.ud_ref = LUA_NOREF;
664+
645665
lua_settop(L, 1);
646666
return 1;
647667
}
@@ -655,6 +675,10 @@ static int lcurl_easy_unset_DEBUGFUNCTION(lua_State *L){
655675
}
656676
curl_easy_setopt(p->curl, CURLOPT_DEBUGDATA, NULL);
657677

678+
luaL_unref(L, LCURL_LUA_REGISTRY, p->debug.cb_ref);
679+
luaL_unref(L, LCURL_LUA_REGISTRY, p->debug.ud_ref);
680+
p->debug.cb_ref = p->debug.ud_ref = LUA_NOREF;
681+
658682
lua_settop(L, 1);
659683
return 1;
660684
}
@@ -670,6 +694,10 @@ static int lcurl_easy_unset_FNMATCH_FUNCTION(lua_State *L){
670694
}
671695
curl_easy_setopt(p->curl, CURLOPT_FNMATCH_DATA, NULL);
672696

697+
luaL_unref(L, LCURL_LUA_REGISTRY, p->match.cb_ref);
698+
luaL_unref(L, LCURL_LUA_REGISTRY, p->match.ud_ref);
699+
p->match.cb_ref = p->match.ud_ref = LUA_NOREF;
700+
673701
lua_settop(L, 1);
674702
return 1;
675703
}
@@ -681,7 +709,14 @@ static int lcurl_easy_unset_CHUNK_BGN_FUNCTION(lua_State *L){
681709
if(code != CURLE_OK){
682710
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
683711
}
684-
// curl_easy_setopt(p->curl, CURLOPT_CHUNK_DATA, NULL);
712+
if(p->chunk_end.cb_ref == LUA_NOREF){
713+
// if other callback not set
714+
curl_easy_setopt(p->curl, CURLOPT_CHUNK_DATA, NULL);
715+
}
716+
717+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_bgn.cb_ref);
718+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_bgn.ud_ref);
719+
p->chunk_bgn.cb_ref = p->chunk_bgn.ud_ref = LUA_NOREF;
685720

686721
lua_settop(L, 1);
687722
return 1;
@@ -693,7 +728,14 @@ static int lcurl_easy_unset_CHUNK_END_FUNCTION(lua_State *L){
693728
if(code != CURLE_OK){
694729
return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code);
695730
}
696-
// curl_easy_setopt(p->curl, CURLOPT_CHUNK_DATA, NULL);
731+
if(p->chunk_bgn.cb_ref == LUA_NOREF){
732+
// if other callback not set
733+
curl_easy_setopt(p->curl, CURLOPT_CHUNK_DATA, NULL);
734+
}
735+
736+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_end.cb_ref);
737+
luaL_unref(L, LCURL_LUA_REGISTRY, p->chunk_end.ud_ref);
738+
p->chunk_end.cb_ref = p->chunk_end.ud_ref = LUA_NOREF;
697739

698740
lua_settop(L, 1);
699741
return 1;

test/test_easy.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,4 +1032,50 @@ end
10321032

10331033
end
10341034

1035+
local _ENV = TEST_CASE'unset_callback_ctx' if ENABLE then
1036+
1037+
local c
1038+
1039+
function setup()
1040+
c = assert(scurl.easy())
1041+
end
1042+
1043+
function teardown()
1044+
if c then c:close() end
1045+
c = nil
1046+
end
1047+
1048+
local function test_cb(name)
1049+
local set, unset = 'setopt_' .. name, 'unsetopt_' .. name
1050+
1051+
set = assert_function(c[set], set)
1052+
unset = assert_function(c[unset], unset)
1053+
1054+
local pctx
1055+
do local ctx = {}
1056+
pctx = weak_ptr(ctx)
1057+
assert(set(c, function() end, ctx))
1058+
end
1059+
1060+
gc_collect()
1061+
assert_table(pctx.value)
1062+
1063+
unset(c)
1064+
1065+
gc_collect()
1066+
assert_nil(pctx.value)
1067+
end
1068+
1069+
function test_read() test_cb('readfunction') end
1070+
function test_write() test_cb('writefunction') end
1071+
function test_header() test_cb('headerfunction') end
1072+
function test_progress() test_cb('progressfunction') end
1073+
function test_seek() test_cb('seekfunction') end
1074+
function test_debug() test_cb('debugfunction') end
1075+
function test_fnmatch() test_cb('fnmatch_function') end
1076+
function test_chunk_bgn() test_cb('chunk_bgn_function') end
1077+
function test_chunk_end() test_cb('chunk_end_function') end
1078+
1079+
end
1080+
10351081
RUN()

0 commit comments

Comments
 (0)