[MDEV-33047] using sql_service within deinit of plugin segfaults on shutdown Created: 2023-12-18  Updated: 2023-12-18

Status: Confirmed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.4, 10.5, 10.6, 10.11
Fix Version/s: 10.4, 10.5

Type: Bug Priority: Major
Reporter: Daniel Black Assignee: Alexey Botchkov
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-27595 Backport SQL service, introduced by M... Closed

 Description   

Using test case with minor deinit adjustment in plugin from MDEV-32808:

(gdb) bt full
#0  0x00007f1bd80ae834 in __pthread_kill_implementation () from /lib64/libc.so.6
No symbol table info available.
#1  0x000000000107f9dc in handle_fatal_signal (sig=<optimized out>)
    at /home/dan/repos/mariadb-server-10.4/sql/signal_handler.cc:369
        curr_time = <optimized out>
        print_invalid_query_pointer = <optimized out>
        thd = 0x52b000093208
        tm = <optimized out>
#2  <signal handler called>
No symbol table info available.
#3  0x0000000000a68e24 in intern_plugin_lock (lex=0x0, rc=0x0, state_mask=14)
    at /home/dan/repos/mariadb-server-10.4/sql/sql_plugin.cc:952
        pi = 0x0
#4  plugin_thdvar_init (thd=0x52b000093208) at /home/dan/repos/mariadb-server-10.4/sql/sql_plugin.cc:3249
        old_table_plugin = 0x0
        old_tmp_table_plugin = 0x0
        old_enforced_table_plugin = 0x0
#5  0x0000000000926976 in THD::init (this=0x52b000093208) at /home/dan/repos/mariadb-server-10.4/sql/sql_class.cc:1214
No locals.
#6  0x00000000009251cb in THD::THD (this=0x52b000093208, id=<optimized out>, is_wsrep_applier=<optimized out>)
    at /home/dan/repos/mariadb-server-10.4/sql/sql_class.cc:809
        old_THR_THD = 0x0
        tmp = <optimized out>
#7  0x0000000000a8f94b in mysql_real_connect_local (mysql=<optimized out>)
    at /home/dan/repos/mariadb-server-10.4/sql/sql_prepare.cc:6207
        thd_orig = 0x0
        client_flag = 131072
        new_thd = <optimized out>
        p = <optimized out>
#8  0x00007f1bd5c04a4d in run_sql (query_len=18, query=<optimized out>)
    at /home/dan/repos/mariadb-server-10.4/plugin/example_keymgt_sql_service/example_keymgt_sql_service.cc:19
        mysql = 0x51a000026a88
#9  example_keymgt_sql_service_deinit (p=<optimized out>)
    at /home/dan/repos/mariadb-server-10.4/plugin/example_keymgt_sql_service/example_keymgt_sql_service.cc:59
No locals.
#10 0x0000000000f3b0dd in finalize_encryption_plugin (plugin=0x521000030e58)
    at /home/dan/repos/mariadb-server-10.4/sql/encryption.cc:121
        used = <optimized out>
#11 0x0000000000a5f643 in plugin_deinitialize (plugin=0x521000030e58, ref_check=<optimized out>)
    at /home/dan/repos/mariadb-server-10.4/sql/sql_plugin.cc:1244
No locals.
#12 0x0000000000a60ec9 in plugin_shutdown () at /home/dan/repos/mariadb-server-10.4/sql/sql_plugin.cc:2104
        count = <optimized out>
        i = 54
        plugin = <optimized out>
        plugins = 0x7ffcefbffc20
        dl = <optimized out>
#13 0x00000000007a10b5 in clean_up (print_message=<optimized out>) at /home/dan/repos/mariadb-server-10.4/sql/mysqld.cc:2010
No locals.
#14 0x00000000007a8621 in mysqld_main (argc=<optimized out>, argv=0x51b000000ee8)
    at /home/dan/repos/mariadb-server-10.4/sql/mysqld.cc:5979
        ho_error = <optimized out>
        new_thread_stack_size = <optimized out>
        user = 0x504000010958 "root[root] @ localhost [127.0.0.1]"
        please_close_stdin = <optimized out>



 Comments   
Comment by Sergei Golubchik [ 2023-12-18 ]

plugin_deinit is invoked too late to run SQL from it.
plugin_init is invoked too early, it's before ddl_log_recovery, so one shouldn't run at least DDLs from it. DMLs might be ok, not sure.

One possible solution would be to introduce "late init" and "early deinit" callbacks. Likely not by changing the plugin structure, but with a separate service. Like

int my_plugin_late_init()
{
  /* ... run SQL from here ... */
}
 
int my_plugin_init()
{
  register_late_init(my_plugin_late_init);
  return 0;
}

One complication to keep in mind — it would encourage plugin writers to run some "finalizing" SQL from the "early deinit" callback. But if the server crashes there will be no callback, plugins need to be able to cope with that.

Generated at Thu Feb 08 10:35:59 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.