[MDEV-19439] Allow THD member (or base) class constructor/destructor to allocate/free memory Created: 2019-05-10  Updated: 2021-03-08

Status: Open
Project: MariaDB Server
Component/s: Server
Fix Version/s: None

Type: Task Priority: Major
Reporter: Sergey Vojtovich Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Currently such attempts lead to assertions like this:

mysqld.cc:3757: void my_malloc_size_cb_func(long long int, my_bool): Assertion `!is_thread_specific || (mysqld_server_initialized && thd)' failed.

The reason is that memory accounting is done in THD constructor (which is called after member/base class constructor). Thus memory allocated by member/base constructor is not accounted. Same problem (but turn it upside down) with destructor.

This causes ugly constructor/destructor simulation like:

THD::~THD()
{
  ...
  /* trick to make happy memory accounting system */
#ifndef EMBEDDED_LIBRARY
  session_tracker.sysvars.deinit();
#endif //EMBEDDED_LIBRARY
 
  if (status_var.local_memory_used != 0)
  {
    DBUG_PRINT("error", ("memory_used: %lld", status_var.local_memory_used));
    SAFEMALLOC_REPORT_MEMORY(thread_id);
    DBUG_ASSERT(status_var.local_memory_used == 0 ||
                !debug_assert_on_not_freed_memory);
  }
  update_global_memory_status(status_var.global_memory_used);
  set_current_thd(orig_thd == this ? 0 : orig_thd);
  DBUG_VOID_RETURN;
}

Original implementation was declined due to convoluted current_thd juggling. This juggling is needed, because in some cases THD objects are created by thread creator, not thread itself.

Patches implementing this task are in bb-10.5-svoj-MDEV-19439, but they are incomplete and fail on assertions. Move THD creation from creator to creature and do whatever is needed to make them work.



 Comments   
Comment by Michael Widenius [ 2019-09-17 ]

Yes, the destructor is a bit ugly, but it's still simple code and only in one place.

As we keep the THD around in the cache for future connections, it's probably good that constructors does not allocate memory as this allows us to reduce the memory for THD's that is in the cache.

Another problem with allocating memory in constructors is that it makes end-of-memory checking much harder when allocating THD.

I greatly prefer to not have any automatic memory allocation in constructors but instead do that in init functions.

Generated at Thu Feb 08 08:51:42 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.