AcademySoftwareFoundation / AcademySoftwareFoundation/OpenShadingLanguage

Crash creating and using OSLCompiler instances in mutliple threads

Open
#1,427 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.3k
Forks
414
Avg merge
3d 1h
Merged PRs (30d)
10

Description

### Problem

In the Cycles renderer we are getting crashes doing different renders in different threads, each creating their own `OSLCompiler` instances. From #654 it appears that this is supposed to work.

I think I understand the cause and am willing to contribute a fix, but looking for advice on the best way to solve this.

What happens is that `OSLCompiler` ends up calling this in its destructor:
```
void
SymbolTable::delete_syms()
{
for (auto& sym : m_allsyms)
delete sym;
m_allsyms.clear();
TypeSpec::struct_list().clear();
}
```

This `TypeSpec::struct_list()` is a global variable used by `OSLCompiler`, and also `ShadingSystem`. So fully clearing it here is not safe.

I can think of a few solutions:
1. Never clear this list and accept the memory usage. `ShadingSystem` does not clear it as far as I can tell, so you already have this when not compiling shaders.
2. Add reference counting to only clear this list when there are zero classes using it. This also involves adding mutex protection in a bunch of places.
3. Add a struct list per `OSLCompiler` and `ShadingSystem`, instead of making it global.

Solution (3) seems like the best to me, does that seem reasonable? Or is there some performance reason that this should be reused.

### Versions

* OSL branch/version: master branch, 6b6637e
* OS: Ubuntu Linux
* C++ compiler: Clang

Contributor guide

Open the contributing guide

Research direction

No file or test is named. Start with SymbolTable::delete_syms() and the shared TypeSpec::struct_list() described in the report, then trace their use by OSLCompiler and ShadingSystem. Done requires an agreed ownership or synchronization design for concurrent instances and evidence that the crash is prevented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, computer-graphics
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.