Skip to content

Commit 83c3d51

Browse files
committed
BUGFIX:
- /node add_instance race condition with new foreign keys
1 parent 3fd0eba commit 83c3d51

File tree

13 files changed

+44
-22
lines changed

13 files changed

+44
-22
lines changed

core/data/impl/nodeimpl.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,17 +1429,11 @@ async def add_instance(self, name: str, *, template: str = "") -> "Instance":
14291429
}
14301430
with open(config_file, mode='w', encoding='utf-8') as outfile:
14311431
yaml.dump(config, outfile)
1432-
settings_path = os.path.join(instance.home, 'Config', 'serverSettings.lua')
1433-
if os.path.exists(settings_path):
1434-
# TODO: dirty cast
1435-
settings = SettingsDict(cast(DataObject, cast(object, self)), settings_path, root='cfg')
1436-
settings['port'] = int(instance.dcs_port)
1437-
settings['name'] = 'n/a'
1438-
settings['missionList'] = []
1439-
settings['listStartIndex'] = 0
14401432
bus = ServiceRegistry.get(ServiceBus)
14411433
server = DataObjectFactory().new(ServerImpl, node=self.node, port=instance.bot_port, name='n/a', bus=bus)
14421434
instance.server = server
1435+
# we cannot use instance.dcs_port here as that would refer to the assigned serverSettings.lua already
1436+
server.settings["port"] = int(instance.locals['dcs_port'])
14431437
self.instances[instance.name] = instance
14441438
bus.servers[server.name] = server
14451439
if not self.master:

core/data/impl/serverimpl.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ def __post_init__(self):
113113
self._lock = asyncio.Lock()
114114
with self.pool.connection() as conn:
115115
with conn.transaction():
116-
conn.execute("INSERT INTO servers (server_name) VALUES (%s) ON CONFLICT (server_name) DO NOTHING",
117-
(self.name, ))
118-
row = conn.execute("SELECT maintenance FROM servers WHERE server_name = %s", (self.name,)).fetchone()
119-
if row:
120-
self._maintenance = row[0]
116+
cursor = conn.execute("""
117+
INSERT INTO servers (server_name)
118+
VALUES (%s)
119+
ON CONFLICT (server_name) DO NOTHING
120+
RETURNING maintenance
121+
""", (self.name, ))
122+
row = cursor.fetchone()
123+
if row:
124+
self._maintenance = row[0]
121125
atexit.register(self.stop_observer)
122126

123127
@override
@@ -141,6 +145,7 @@ def settings(self) -> dict:
141145
# if someone managed to destroy the mission list, fix it...
142146
if 'missionList' not in self._settings:
143147
self._settings['missionList'] = []
148+
self._settings['listStartIndex'] = 0
144149
elif isinstance(self._settings['missionList'], dict):
145150
self._settings['missionList'] = list(self._settings['missionList'].values())
146151
return self._settings
@@ -447,9 +452,11 @@ async def update_database(old_name: str, new_name: str):
447452
# rename the server in the database
448453
async with self.apool.connection() as conn:
449454
async with conn.transaction():
450-
# we need to remove any older server that might have had the same name
451455
await conn.execute("UPDATE servers SET server_name = %s WHERE server_name = %s",
452-
(new_name, old_name))
456+
(new_name, old_name or 'n/a'))
457+
if not old_name:
458+
await conn.execute("UPDATE instances SET server_name = %s WHERE instance = %s",
459+
(new_name, self.instance.name))
453460

454461
async def update_cluster(new_name: str):
455462
# only the master can take care of a cluster-wide rename
@@ -484,6 +491,13 @@ async def update_cluster(new_name: str):
484491
except Exception:
485492
self.log.exception(f"Error during renaming of server {old_name} to {new_name}: ", exc_info=True)
486493

494+
async def unlink(self):
495+
if self.name == 'n/a':
496+
async with self.apool.connection() as conn:
497+
async with conn.transaction():
498+
await conn.execute("DELETE FROM servers WHERE server_name = 'n/a'")
499+
self.instance.server = None
500+
487501
@performance_log()
488502
def do_startup(self):
489503
basepath = self.node.installation

core/data/proxy/serverproxy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ async def rename(self, new_name: str, update_settings: bool = False) -> None:
234234
}, node=self.node.name, timeout=timeout)
235235
self.name = new_name
236236

237+
@override
238+
async def unlink(self):
239+
await self.bus.send_to_node_sync({
240+
"command": "rpc",
241+
"object": "Server",
242+
"method": "unlink",
243+
"server_name": self.name
244+
}, node=self.node.name, timeout=60)
245+
237246
@override
238247
async def render_extensions(self) -> list[dict]:
239248
if not self._extensions:

core/data/server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ async def send_to_dcs(self, message: dict):
293293
async def rename(self, new_name: str, update_settings: bool = False) -> None:
294294
raise NotImplementedError()
295295

296+
@abstractmethod
297+
async def unlink(self):
298+
raise NotImplementedError()
299+
296300
@abstractmethod
297301
async def startup(self, modify_mission: bool | None = True, use_orig: bool | None = True) -> None:
298302
raise NotImplementedError()

locale/cn/LC_MESSAGES/admin.mo

0 Bytes
Binary file not shown.

locale/cn/LC_MESSAGES/admin.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ msgstr "该实例正在被服务器使用 \"{}\".\n"
362362
msgid "Do you really want to delete instance {}?"
363363
msgstr "你真的想删除实例 {} 吗?"
364364

365-
msgid "Do you want to remove the directory {}?"
365+
msgid "Do you want to remove the directory\n{}?"
366366
msgstr "你想删除目录 {} 吗?"
367367

368368
msgid "Instance {instance} removed from node {node}."

locale/de/LC_MESSAGES/admin.mo

0 Bytes
Binary file not shown.

locale/de/LC_MESSAGES/admin.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ msgstr "Die Instanz wird zurzeit vom Server \"{}\" genutzt.\n"
362362
msgid "Do you really want to delete instance {}?"
363363
msgstr "Möchtest Du Instanz {} wirklich löschen?"
364364

365-
msgid "Do you want to remove the directory {}?"
365+
msgid "Do you want to remove the directory\n{}?"
366366
msgstr "Möchtest Du das Verzeichnis {} auch löschen?"
367367

368368
msgid "Instance {instance} removed from node {node}."

locale/es/LC_MESSAGES/admin.mo

0 Bytes
Binary file not shown.

locale/es/LC_MESSAGES/admin.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ msgstr "La instancia está en uso por el servidor \"{}\".\n"
356356
msgid "Do you really want to delete instance {}?"
357357
msgstr "¿Seguro que desea eliminar la instancia {}?"
358358

359-
msgid "Do you want to remove the directory {}?"
360-
msgstr "Desea eliminar el directorio {}?"
359+
msgid "Do you want to remove the directory\n{}?"
360+
msgstr "Desea eliminar el directorio\n{}?"
361361

362362
msgid "Instance {instance} removed from node {node}."
363363
msgstr "Instance {instance} eliminada del nodo {node}."

0 commit comments

Comments
 (0)