Skip to content

Commit 13e59be

Browse files
committed
Fix ASAN crash by avoiding decimal module in import tests
The _decimal C extension has global state (mpd_setminalloc) that cannot handle being loaded in multiple interpreters. When the import registry contained decimal, it would be applied to subinterpreters causing memory corruption detected by AddressSanitizer. Replace decimal with textwrap (pure Python) in import_in_sys_modules_test.
1 parent a767346 commit 13e59be

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

test/py_import_SUITE.erl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,26 +638,30 @@ registry_applied_to_subinterp_test(_Config) ->
638638
%% After calling py:import, the module should be in the interpreter's
639639
%% sys.modules dictionary. We verify this by checking that calling
640640
%% a function from the module works (which requires it to be imported).
641+
%%
642+
%% Note: We use textwrap (pure Python) instead of decimal because the
643+
%% _decimal C extension has global state that crashes in subinterpreters.
641644
import_in_sys_modules_test(_Config) ->
642645
%% Clear registry
643646
ok = py_import:clear_imports(),
644647

645-
%% Import a module
646-
ok = py_import:ensure_imported(decimal),
648+
%% Import a pure Python module (avoid C extensions like decimal
649+
%% which have global state that crashes in subinterpreters)
650+
ok = py_import:ensure_imported(textwrap),
647651

648652
%% Verify the import worked by calling a function
649-
{ok, _} = py:call(decimal, 'Decimal', [<<"3.14">>]),
653+
{ok, _} = py:call(textwrap, fill, [<<"Hello world">>, 5]),
650654

651655
%% Now check sys.modules using the same process (important!)
652656
%% We use exec to define a helper, then eval to check
653657
ok = py:exec(<<"
654658
import sys
655-
_test_decimal_in_sys = 'decimal' in sys.modules
659+
_test_textwrap_in_sys = 'textwrap' in sys.modules
656660
">>),
657-
{ok, InSysModules} = py:eval(<<"_test_decimal_in_sys">>),
661+
{ok, InSysModules} = py:eval(<<"_test_textwrap_in_sys">>),
658662
?assertEqual(true, InSysModules),
659663

660-
ct:pal("py:import correctly adds module to sys.modules").
664+
ct:pal("py_import:ensure_imported correctly adds module to sys.modules").
661665

662666
%% @doc Test that ETS registry and sys.modules stay in sync
663667
%%

0 commit comments

Comments
 (0)