Skip to content

fix: possible double free due to multithreading with Contour - #3496

Merged
ice0 merged 4 commits into
synfig:masterfrom
rodolforg:fix-random-crash-freeing-contour
Mar 3, 2025
Merged

ice0 merged 4 commits into
synfig:masterfrom
rodolforg:fix-random-crash-freeing-contour

Conversation

@rodolforg

Copy link
Copy Markdown
Contributor

Random crashes :'(

fix #3493

@rodolforg
rodolforg requested a review from ice0 February 22, 2025 23:39
@github-actions

Copy link
Copy Markdown

Thank you for contributing!
Below is how your contribution will appear in Synfig's release notes. Review it and improve your pull request's title and description so that your contribution is clear. You can add images, videos, and anything else you want. Have fun!

🐞 Bug Fixes

@rodolforg

rodolforg commented Feb 22, 2025 •

Copy link
Copy Markdown
Contributor Author

Maybe change

class Contour: public etl::shared_object
{
public:
	typedef etl::handle<Contour> Handle;

to

class Contour
{
public:
	typedef std::shared_ptr<Contour> Handle;

?


public:
Contour::Handle contour;
std::shared_ptr<Contour> contour;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use std::unique_ptr, but couldn't compile because some Task copy calls... I didn't investigate it though

@ice0 ice0 Mar 2, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't remember, but maybe you know - Task is always unique, or copies are possible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... They can be set as sub_task of one or more tasks...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But contour itself I don't know if it is shared to other stuff besides the task.

@ice0

ice0 commented Mar 2, 2025

Copy link
Copy Markdown
Collaborator

Maybe change

class Contour: public etl::shared_object
{
public:
	typedef etl::handle<Contour> Handle;

to

class Contour
{
public:
	typedef std::shared_ptr<Contour> Handle;

?

I tried, but it affects other parts of the code. You can try too, maybe you'll have a better luck.

@ice0

ice0 commented Mar 2, 2025

Copy link
Copy Markdown
Collaborator

I didn't merge because Synfig is still crashing. I'm currently looking into why this is happening.

@rodolforg

rodolforg commented Mar 2, 2025 •

Copy link
Copy Markdown
Contributor Author

Still due to double free problem?

…more

Better use safer std::shared_ptr class from standard library.
@rodolforg
rodolforg force-pushed the fix-random-crash-freeing-contour branch from 2064155 to dc31e9d Compare March 2, 2025 12:50
@rodolforg

Copy link
Copy Markdown
Contributor Author

I particularly do not agree with this:

{
std::lock_guard<std::mutex> lock(other.intersector_read_mutex);
intersector = other.intersector;
}

The code is assigning a Contour to another Contour object, but ties the Intersector, instead of copying it.

@ice0

ice0 commented Mar 2, 2025

Copy link
Copy Markdown
Collaborator

Still due to double free problem?

I am not sure.
To reproduce, run looped animation and zoom in/zoom out work area until it crashes.

This is call stack I got:

image

@rodolforg

rodolforg commented Mar 2, 2025 •

Copy link
Copy Markdown
Contributor Author

Hm... I believe it is due to double free too...
Maybe we should std::shared_ptr to tasks too? (btw, the only thing that currently needs these ETL classes is ValueNode - to easily replace a valuenode via etl::rhandle).

@rodolforg

Copy link
Copy Markdown
Contributor Author

What if this code of etl::shared_object::unref():

synfig/ETL/ETL/_handle.h

Lines 100 to 105 in 5184864

virtual void unref() const
{
--refcount;
if (refcount == 0)
delete this;
}

was like this:

	virtual void unref() const
	{
		if (--refcount == 0)
			delete this;
	}

@ice0

ice0 commented Mar 3, 2025

Copy link
Copy Markdown
Collaborator

What if this code of etl::shared_object::unref():

Nice catch!
After making the specified changes, the issue no longer occurs.
I spent three minutes trying to reproduce the problem, but everything worked without errors.
Awesome!

@ice0

ice0 commented Mar 3, 2025

Copy link
Copy Markdown
Collaborator

Maybe we should std::shared_ptr to tasks too?

I agree :)

Avoid this situation:

refcount = 2

Thread 1: decrement -> refcount = 1
Thread 2: decrement -> refcount = 0
Thread 1: compare (true) -> self delete
Thread 2: compare (true) -> self delete
double free or segfault
@rodolforg

Copy link
Copy Markdown
Contributor Author

Done the unref() stuff :)

@ice0
ice0 merged commit 3d9c023 into synfig:master Mar 3, 2025
@ice0

ice0 commented Mar 3, 2025

Copy link
Copy Markdown
Collaborator

Merged. Thank you!

@rodolforg
rodolforg deleted the fix-random-crash-freeing-contour branch March 3, 2025 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Random Crashes on Version 1.5.3

2 participants