Skip to content

Commit bdccf90

Browse files
author
Alex Smith
committed
Potential fix for the watching desyncs
Untested, because I can't reproduce them on this computer. I'll get one of the other devs to test it.
1 parent 72f39d5 commit bdccf90

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

libnethack/src/log.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* vim:set cin ft=c sw=4 sts=4 ts=8 et ai cino=Ls\:0t0(0 : -*- mode:c;fill-column:80;tab-width:8;c-basic-offset:4;indent-tabs-mode:nil;c-file-style:"k&r" -*-*/
2-
/* Last modified by Alex Smith, 2015-07-21 */
2+
/* Last modified by Alex Smith, 2016-07-07 */
33
/* Copyright (c) Daniel Thaler, 2011. */
44
/* NetHack may be freely redistributed. See license for details. */
55

@@ -2403,18 +2403,38 @@ log_replay_save_line(void)
24032403
char *logline;
24042404
struct memfile bsave;
24052405

2406-
if (!change_fd_lock(program_state.logfile, TRUE, LT_READ, 2))
2407-
panic("Could not upgrade to read lock on logfile");
2406+
int tries = 30;
24082407

2409-
lseek(program_state.logfile,
2410-
program_state.end_of_gamestate_location, SEEK_SET);
2408+
/* The file should always end with a save line. If we don't have one,
2409+
either the process got interrupted or we outraced the process that
2410+
was meant to write it. */
2411+
do {
24112412

2412-
logline = lgetline_malloc(program_state.logfile);
2413+
if (!change_fd_lock(program_state.logfile, TRUE, LT_READ, 2))
2414+
panic("Could not upgrade to read lock on logfile");
24132415

2414-
if (!change_fd_lock(program_state.logfile, TRUE, LT_MONITOR, 2))
2415-
panic("Could not downgrade to monitor lock on logfile");
2416+
lseek(program_state.logfile,
2417+
program_state.end_of_gamestate_location, SEEK_SET);
2418+
2419+
logline = lgetline_malloc(program_state.logfile);
2420+
2421+
if (!change_fd_lock(program_state.logfile, TRUE, LT_MONITOR, 2))
2422+
panic("Could not downgrade to monitor lock on logfile");
2423+
2424+
#ifndef WIN32
2425+
/* Don't bother sleeping on Windows; this situation should be
2426+
impossible anyway because watching doesn't work there */
2427+
if (!logline)
2428+
nanosleep(&(struct timespec){.tv_nsec = 100000000}, NULL);
2429+
#endif
2430+
2431+
} while (!logline && tries--);
2432+
2433+
if (!logline)
2434+
return; /* will probably cause an error if watching but that's what we
2435+
want */
24162436

2417-
if (logline && *logline == '~') {
2437+
if (*logline == '~') {
24182438

24192439
bsave = program_state.binary_save;
24202440
program_state.binary_save_allocated = FALSE;
@@ -2424,15 +2444,15 @@ log_replay_save_line(void)
24242444
program_state.end_of_gamestate_location;
24252445
load_gamestate_from_binary_save(TRUE);
24262446

2427-
} else if (logline && *logline == '*') {
2447+
} else if (*logline == '*') {
24282448

24292449
load_save_backup_from_string(logline);
24302450
program_state.binary_save_location =
24312451
program_state.save_backup_location =
24322452
program_state.end_of_gamestate_location;
24332453
load_gamestate_from_binary_save(TRUE);
24342454

2435-
} else if (logline && *logline == 'Q') {
2455+
} else if (*logline == 'Q') {
24362456

24372457
terminate(GAME_ALREADY_OVER);
24382458

0 commit comments

Comments
 (0)