GithubHelp home page GithubHelp logo

Comments (7)

joente avatar joente commented on August 30, 2024 2

You are right, I missed this one.. Actually, I think replacing the uint32_t ref with atomic_uint ref should be enough to fix this issue since only one thread will decrement the ref count to zero. Do you have the option to try this fix?

from siridb-server.

ubnt-michals avatar ubnt-michals commented on August 30, 2024 1

I am testing #159 - basically the same thing. I'll let it run overnight.

from siridb-server.

ubnt-michals avatar ubnt-michals commented on August 30, 2024

@joente I had some time to investigate the core dump a little bit more. It looks like some kind of memory corruption.

If you look at the iteration at:

for (; q_wrapper->vec_index < index_end; q_wrapper->vec_index++)
{
series = (siridb_series_t *)
q_wrapper->vec->data[q_wrapper->vec_index];
pcre_exec_ret = pcre2_match(
q_wrapper->regex,
(PCRE2_SPTR8) series->name,
series->name_len,
0, /* start looking at this point */
0, /* OPTIONS */
q_wrapper->match_data,
0); /* length of sub_str_vec */
if ( pcre_exec_ret < 0 ||
imap_add(q_wrapper->series_tmp, series->id, series))
{
siridb_series_decref(series);
}
}

The series struct looks like a garbage memory:

image

The culprit, in this case, is name_len that (I think) makes no sense. Although the memory looks corrupted, it's properly allocated. The pcre2_match it's just slow going over a string that is 22kb long. Looks like all this is not synced to the disk because server restart helps for a couple of hours.

from siridb-server.

ubnt-michals avatar ubnt-michals commented on August 30, 2024

@joente Could you please give me any pointers on where to start investigating? This is plaguing most of our instances. I can definitely allocate time to fix this, but I am not sure where to start looking for possible corruption.

We don't do anything fancy, 90% of our queries are inserts, the rest is select mean()/min()/max() with explicit series or regexp. There doesn't seem to be an easy repro case.

from siridb-server.

joente avatar joente commented on August 30, 2024

@ubnt-michals, I'm not entirely sure where it goes wrong but I will look into this issue and hope to give you an answer soon. Do you know if the issue disappears once you eliminate regexp queries and stick to fixed or tagged series?

from siridb-server.

ubnt-michals avatar ubnt-michals commented on August 30, 2024

@joente @luznicky
Some more observations:

The corruption probably doesn't happen by deleting a series.

image

In this case q_wrapper->vec->data[6000] is the same pointer as siridb->series_map->vec->data[6000]. Deleting a series would cause the siridb->series_map->vec to be set to NULL. Also both vecs have the same len.

In this core dump, a total of 3 series indexed 5998, 5999, 6000 looks corrupt. The data in them are not totally random.

image
image
image

For example, name_len is 22032 in all of them.

Also, the fact that they are allocated next to each other points me to some buffer overrun.

from siridb-server.

ubnt-michals avatar ubnt-michals commented on August 30, 2024

I think we have found a potential culprit of this memory corruption.

From the looks of it, the reference counting is not thread-safe.

The same locking that is for example here:

uv_mutex_lock(&siridb->series_mutex);
vec_t * vec = imap_2vec_ref(siridb->series_map);
uv_mutex_unlock(&siridb->series_mutex);

Is not here around siridb_series_decref

siridb_series_decref(series);

The "optimize" task runs in a different thread, and siridb_series_incref and siridb_series_decref are not atomic. Therefore they should create a race condition and use after free would explain what we see in the core dumps.

@joente can you confirm that that's the case? Maybe we are missing something and the use of siridb_series_incref/siridb_series_decref is not a problem.

from siridb-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.