Skip to content

Commit 4b17105

Browse files
Rogier-5paramat
authored andcommittedOct 16, 2016
Emergeblocks: Fix occasional crash
Modification of the emergeblocks internal state was not protected by a lock, causing a race condition. This can be reproduced by repeatedly running emergeblocks for an already-generated section of the map (with multiple emerge threads).
1 parent adad6e0 commit 4b17105

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed
 

Diff for: ‎src/script/cpp_api/s_env.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,13 @@ void ScriptApiEnv::on_emerge_area_completion(
212212
{
213213
Server *server = getServer();
214214

215+
// This function should be executed with envlock held.
216+
// The caller (LuaEmergeAreaCallback in src/script/lua_api/l_env.cpp)
217+
// should have obtained the lock.
215218
// Note that the order of these locks is important! Envlock must *ALWAYS*
216219
// be acquired before attempting to acquire scriptlock, or else ServerThread
217220
// will try to acquire scriptlock after it already owns envlock, thus
218221
// deadlocking EmergeThread and ServerThread
219-
MutexAutoLock envlock(server->m_env_mutex);
220222

221223
SCRIPTAPI_PRECHECKHEADER
222224

Diff for: ‎src/script/lua_api/l_env.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ void LuaEmergeAreaCallback(v3s16 blockpos, EmergeAction action, void *param)
137137
assert(state->script != NULL);
138138
assert(state->refcount > 0);
139139

140+
// state must be protected by envlock
141+
Server *server = state->script->getServer();
142+
MutexAutoLock envlock(server->m_env_mutex);
143+
140144
state->refcount--;
141145

142146
state->script->on_emerge_area_completion(blockpos, action, state);

0 commit comments

Comments
 (0)
Please sign in to comment.