Keep the state file's line ahead of RivaTuner Statistics Server's profile, and keep what a run owes the profile after a put back that did not land.

The review found three ways rtss-profile.txt could fall behind the profile. Every write after a run's first one saved
the profile before the line, so a kill between the two left a line whose value the profile no longer held, and the
next start took the game's limit for the player's (rtss_restore_plan LEAVE). A put back whose write did not land kept
the line, but rtss_put_back_done cleared S.changed, so a later limit took the run's first-write path again and read the
game's own limit from the profile as the value to put back, overwriting the true one in the line. And put_back removed
the line after DeleteProfile without checking that the profile went away.

The line now carries the value the run wrote before the newest one (rtss_decide.h RtssSaved prev_limit and prev_den,
rtss1 with two more fields; the file came with this branch and was never released). rtss_limit.c apply writes the new
value into the line before the profile (record_before) and drops the previous one once the profile holds the new one,
or returns to it when RTSS refused it (record_after), and rtss_restore_plan takes a profile holding either value for
the game's. A new owed flag keeps had_profile, old_limit and old_denominator from the run's first write until a put
back lands: a later first write keeps them and the line's values, a failed first write leaves the line alone, and
rtss_limit_restore tries the put back again at the exit. put_back and restore_left check that a deleted profile is
gone before they remove the line, and restore_left's lines name the value the profile held. tests/rtss_limit_check.c
covers the new fields and the plan for either value; tests/rtss_worker_check.c checks the line before and after a
write (state_record), a run killed inside the save of its second limit and the next start removing the profile
(kill_restart_mid_write), a put back that did not land followed by a limit and the exit (put_back_owed), and a removal
that did not land (put_back_undeleted). Lines over 120 columns from the earlier fixes are wrapped.

Not reproduced before the change. Checks that passed: tests/rtss_limit_check.py (44 of 44), tests/exit_order_check.py,
tests/fatal_exit_check.py, pass_cc_check.py --wall on rtss_limit.c, tools/check_content.py --staged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 19:52:29 -05:00
parent 1744681f1a
commit da822abec3
5 changed files with 194 additions and 74 deletions

View File

@@ -469,8 +469,8 @@ void rtss_limit_want(int64_t mhz); // the pacer's limit i
void rtss_limit_movie(int hz); // the rate of a movie that plays, 0 none: the pacer holds it
int rtss_limit_active(void); // RTSS limits to the wanted rate: the pacer keeps the ceiling
int rtss_limit_state(void); // menu: 1 RTSS limits, 2 its profile is read-only, 3 it let the
// game run past its limit or never showed it holds it, 4 kept
// out while a limit set in RTSS itself holds the game too
// game run past its limit or never showed it holds it, 4
// kept out while a limit set in RTSS holds the game too
void rtss_limit_restore(void); // before the process ends: the profile's old limit back
void rtss_limit_crash(void); // a crash or fatal error: no call into RTSS, the next start
// puts the profile back (rtss-profile.txt)

View File

@@ -341,25 +341,29 @@ static inline uint64_t rtss_pace_period(int holds, int movie, uint64_t now_ns, u
// ---- the state file (rtss_limit.c, rtss-profile.txt next to the executable): each RTSS profile a run changed, as it
// was before, so that a start after a run that did not close (a kill, a hang, a crash) puts it back. One line per
// profile, so executables that share the folder keep their own:
// rtss1 <existed 0|1> <old limit> <old denominator> <written limit> <written denominator> <profile name>
// The name comes last and may hold spaces; lines that start with # are comments.
// rtss1 <existed 0|1> <old limit> <old denominator> <written limit> <written denominator> <previous limit>
// <previous denominator> <profile name>
// on one line. The name comes last and may hold spaces; lines that start with # are comments. The line takes a new
// value before the profile does, and the value written before it stays as the previous one until the profile holds the
// new one: a run that ends between the two leaves either value in the profile, and both are the game's.
#define RTSS_SAVED_HEADER \
"# RivaTuner Statistics Server's profiles as they were before the game changed them (rtss_limit.c): existed, old\n" \
"# limit and denominator, the game's limit and denominator, profile. The game puts a profile back and removes its\n" \
"# line when it closes; a start that finds a line puts that profile back first.\n"
"# limit and denominator, the game's limit and denominator, the game's previous ones, profile. The game puts a\n" \
"# profile back and removes its line when it closes; a start that finds a line puts that profile back first.\n"
enum { RTSS_NAME_MAX = 260 };
typedef struct {
int existed; // the profile existed before the run changed it
uint32_t old_limit, old_den; // what it held then
uint32_t limit, den; // what the run wrote last
uint32_t prev_limit, prev_den; // what it wrote before, while the profile may still hold it; else limit and den
char name[RTSS_NAME_MAX]; // the profile's name, the executable's (ocarina.exe)
} RtssSaved;
// The line of r, with its newline: its length, 0 when it does not fit n.
static inline size_t rtss_saved_format(char *out, size_t n, const RtssSaved *r) {
int k = snprintf(out, n, "rtss1 %d %u %u %u %u %s\n", r->existed ? 1 : 0, r->old_limit, r->old_den, r->limit,
r->den, r->name);
int k = snprintf(out, n, "rtss1 %d %u %u %u %u %u %u %s\n", r->existed ? 1 : 0, r->old_limit, r->old_den,
r->limit, r->den, r->prev_limit, r->prev_den, r->name);
return k > 0 && (size_t)k < n ? (size_t)k : 0;
}
@@ -371,11 +375,14 @@ static inline int rtss_saved_parse(const char *line, size_t len, RtssSaved *r) {
memcpy(buf, line, len);
buf[len] = 0;
int existed = -1, at = 0;
unsigned v[4];
if (sscanf(buf, "rtss1 %d %u %u %u %u %n", &existed, &v[0], &v[1], &v[2], &v[3], &at) != 5 || !at) return 0;
unsigned v[6];
if (sscanf(buf, "rtss1 %d %u %u %u %u %u %u %n", &existed, &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &at) != 7 ||
!at)
return 0;
size_t name = len - (size_t)at;
if ((existed != 0 && existed != 1) || !v[1] || !v[3] || !name || name >= RTSS_NAME_MAX) return 0;
if ((existed != 0 && existed != 1) || !v[1] || !v[3] || !v[5] || !name || name >= RTSS_NAME_MAX) return 0;
r->existed = existed, r->old_limit = v[0], r->old_den = v[1], r->limit = v[2], r->den = v[3];
r->prev_limit = v[4], r->prev_den = v[5];
memcpy(r->name, buf + at, name + 1);
return 1;
}
@@ -430,7 +437,7 @@ static inline size_t rtss_saved_update(const char *text, const char *name, const
}
// What a start does with a line a run that did not close left (r), given the profile as it is now: whether it exists
// and the limit and denominator it holds.
// and the limit and denominator it holds. Either value of the line is the game's (RtssSaved.prev_limit).
enum {
RTSS_PLAN_NONE, // no profile, and there was none before that run: as it was
RTSS_PLAN_LEAVE, // the player changed or removed the profile since: theirs as it is
@@ -439,7 +446,8 @@ enum {
};
static inline int rtss_restore_plan(const RtssSaved *r, int exists, uint32_t limit, uint32_t den) {
if (!exists) return r->existed ? RTSS_PLAN_LEAVE : RTSS_PLAN_NONE;
if (limit != r->limit || (den ? den : 1) != r->den) return RTSS_PLAN_LEAVE;
den = den ? den : 1;
if ((limit != r->limit || den != r->den) && (limit != r->prev_limit || den != r->prev_den)) return RTSS_PLAN_LEAVE;
return r->existed ? RTSS_PLAN_WRITE : RTSS_PLAN_DELETE;
}

View File

@@ -80,6 +80,7 @@ static RtssState S = RTSS_STATE_INIT; // the worker's decisions (rtss_decide.h
static int has_denominator; // this RTSS knows FramerateLimitDenominator
static int had_profile; // the profile existed before this run changed it
static DWORD old_limit, old_denominator = 1;
static int owed; // this run changed the profile and has not put it back: the three above stay as found
static int creations, removals; // the profile's "created" and "removed" lines this run, numbered (under lock)
static char state_path[MAX_PATH + 32]; // rtss-profile.txt next to the executable; "" when its path is unknown
static RtssSaved rec; // this run's line in it, while the profile holds a value of this run
@@ -197,20 +198,30 @@ static void unrecorded(void) {
"Server's profile %s", *state_path ? state_path : "rtss-profile.txt", profile);
}
// rec's written value is limit/denominator now: the line follows it, so that the next start tells the game's value from
// a value the player set since.
static void record(DWORD limit, DWORD denominator) {
// Before a write of limit/denominator into the profile: rec's written value is that one, and the one written before it
// stays as the previous one (rtss_decide.h RtssSaved), so that a run that ends before the profile changed, or after,
// leaves a profile the next start tells from a value the player set since.
static void record_before(DWORD limit, DWORD denominator) {
if (!atomic_load(&recorded) || (rec.limit == limit && rec.den == denominator)) return;
rec.prev_limit = rec.limit, rec.prev_den = rec.den;
rec.limit = limit, rec.den = denominator;
if (!state_put(&rec)) unrecorded();
}
// After that write: the profile holds the written value alone (landed), or kept the previous one.
static void record_after(int landed) {
if (!atomic_load(&recorded) || (rec.limit == rec.prev_limit && rec.den == rec.prev_den)) return;
if (landed) rec.prev_limit = rec.limit, rec.prev_den = rec.den;
else rec.limit = rec.prev_limit, rec.den = rec.prev_den;
if (!state_put(&rec)) unrecorded();
}
// Writes mhz into the profile (rtss_poll's RtssOps.apply): RTSS_WROTE_*. first: the run's first write, which records
// what to put back on exit, in the state file too before anything changes; clear: on a refusal, clear the limit of this
// run the profile holds.
static int apply(void *ctx, int64_t mhz, int first, int clear) {
(void)ctx;
if (first) { // what to put back on exit
if (first && !owed) { // what to put back on exit; after a put back that did not land, the profile still owes it
had_profile = profile_exists();
load_profile(profile);
old_limit = 0, old_denominator = 1;
@@ -222,18 +233,24 @@ static int apply(void *ctx, int64_t mhz, int first, int clear) {
rtss_limit_value(mhz, has_denominator, &l, &d);
DWORD limit = l, denominator = d;
if (first) { // the line goes in before the profile changes: a kill between the two finds it as it was
rec = (RtssSaved){had_profile, old_limit, old_denominator, limit, denominator, ""};
RtssSaved was = rec; // still in the file after a put back that did not land: the profile holds its value
int kept = atomic_load(&recorded);
rec = (RtssSaved){had_profile, old_limit, old_denominator, limit, denominator, kept ? was.limit : limit,
kept ? was.den : denominator, ""};
snprintf(rec.name, sizeof rec.name, "%s", profile);
if (state_put(&rec)) atomic_store(&recorded, 1);
else unrecorded();
} else {
record_before(limit, denominator);
}
int r = write_limit(limit, denominator);
if (r < 0) {
if (r < 0) { // the line keeps both values: the profile may hold either
rt_log("frame rate: RivaTuner Statistics Server's profile %s did not keep the limit: its Profiles folder needs the "
"game run as administrator", profile);
if (first && atomic_exchange(&recorded, 0)) state_put(NULL); // the profile is as it was
if (first && !owed && atomic_exchange(&recorded, 0)) state_put(NULL); // the profile is as it was
return RTSS_WROTE_READONLY;
}
owed = 1;
if (first) {
if (!had_profile) // numbered: a want that flips between a limit and kept out creates it again at each flip
rt_log("frame rate: RivaTuner Statistics Server profile %s created; it goes away when the game closes "
@@ -244,14 +261,16 @@ static int apply(void *ctx, int64_t mhz, int first, int clear) {
}
if (r == 0) { // RTSS must not keep an earlier limit of this run under the game's own one
rt_log("frame rate: RivaTuner Statistics Server refused a limit of %.3f fps", mhz / 1000.0);
if (clear && write_limit(0, 1) > 0) {
record(0, 1);
update_profiles();
return RTSS_WROTE_REFUSED_CLEARED;
}
return RTSS_WROTE_REFUSED;
record_after(0);
if (!clear) return RTSS_WROTE_REFUSED;
record_before(0, 1);
int cleared = write_limit(0, 1) > 0;
record_after(cleared);
if (!cleared) return RTSS_WROTE_REFUSED;
update_profiles();
return RTSS_WROTE_REFUSED_CLEARED;
}
record(limit, denominator);
record_after(1);
update_profiles();
if (!mhz) rt_log("frame rate: RivaTuner Statistics Server's profile %s has no limit", profile);
else if ((int64_t)limit * 1000 == mhz * denominator)
@@ -262,14 +281,19 @@ static int apply(void *ctx, int64_t mhz, int first, int clear) {
return RTSS_WROTE_HELD;
}
// The profile as it was before this run changed it (the caller checked S.changed, and marks it done). Its line in the
// state file goes once it is back; one that did not take its old limit keeps it for the next start.
// The profile as it was before this run changed it (the caller checked S.changed or owed, and marks S done). Its line
// in the state file goes once it is back; one that did not go back keeps the line, and owed, for the exit and the next
// start.
static void put_back(void) {
int back = 1;
if (!had_profile) {
delete_profile(profile);
rt_log("frame rate: RivaTuner Statistics Server profile %s removed, as before the game started "
"(removal %d this run)", profile, ++removals);
back = !profile_exists();
if (back)
rt_log("frame rate: RivaTuner Statistics Server profile %s removed, as before the game started "
"(removal %d this run)", profile, ++removals);
else
rt_log("frame rate: RivaTuner Statistics Server profile %s did not go away", profile);
} else if (write_limit(old_limit, old_denominator) > 0) {
rt_log("frame rate: RivaTuner Statistics Server profile %s has its limit of %lu/%lu back", profile, old_limit,
old_denominator);
@@ -278,6 +302,7 @@ static void put_back(void) {
back = 0;
}
update_profiles();
if (back) owed = 0;
if (back && atomic_exchange(&recorded, 0)) state_put(NULL);
}
@@ -292,30 +317,30 @@ static int left_line(RtssSaved *r) {
// The line r of this profile, left in the state file by a run that did not close (left_start): before this run changes
// anything, the profile gets back what it held before that run (rtss_restore_plan), unless the player changed it since,
// and the line goes. A profile that does not take its old limit keeps the line, and this run leaves the file alone.
// and the line goes. A profile that does not go back keeps the line, and this run leaves the file alone.
static void restore_left(const RtssSaved *r) {
int exists = profile_exists();
load_profile(profile);
DWORD limit = 0, den = 1;
get_property("FramerateLimit", (LPBYTE)&limit, sizeof limit);
has_denominator = get_property("FramerateLimitDenominator", (LPBYTE)&den, sizeof den);
if (!den) den = 1;
int plan = rtss_restore_plan(r, exists, limit, den);
if (plan == RTSS_PLAN_WRITE && write_limit(r->old_limit, r->old_den) <= 0) {
rt_log("frame rate: RivaTuner Statistics Server profile %s still has the game's limit of %u/%u from a run that "
"did not close, and did not take its old limit back; %s keeps its line", profile, r->limit, r->den,
state_path);
if (plan == RTSS_PLAN_DELETE) delete_profile(profile);
if ((plan == RTSS_PLAN_WRITE && write_limit(r->old_limit, r->old_den) <= 0) ||
(plan == RTSS_PLAN_DELETE && profile_exists())) {
rt_log("frame rate: RivaTuner Statistics Server profile %s still has the game's limit of %lu/%lu from a run "
"that did not close, and did not go back as it was; %s keeps its line", profile, limit, den, state_path);
state_off = 1;
return;
}
if (plan == RTSS_PLAN_DELETE) delete_profile(profile);
if (plan == RTSS_PLAN_WRITE || plan == RTSS_PLAN_DELETE) update_profiles();
if (plan == RTSS_PLAN_WRITE)
rt_log("frame rate: RivaTuner Statistics Server profile %s had the game's limit of %u/%u from a run that did not "
"close (%s); its limit of %u/%u is back", profile, r->limit, r->den, state_path, r->old_limit,
r->old_den);
rt_log("frame rate: RivaTuner Statistics Server profile %s had the game's limit of %lu/%lu from a run that did "
"not close (%s); its limit of %u/%u is back", profile, limit, den, state_path, r->old_limit, r->old_den);
else if (plan == RTSS_PLAN_DELETE)
rt_log("frame rate: RivaTuner Statistics Server profile %s had the game's limit of %u/%u from a run that did not "
"close (%s); removed, as before that run", profile, r->limit, r->den, state_path);
rt_log("frame rate: RivaTuner Statistics Server profile %s had the game's limit of %lu/%lu from a run that did "
"not close (%s); removed, as before that run", profile, limit, den, state_path);
else if (plan == RTSS_PLAN_LEAVE)
rt_log("frame rate: RivaTuner Statistics Server profile %s changed since a run that did not close (%s); left as "
"it is", profile, state_path);
@@ -579,7 +604,7 @@ void rtss_limit_restore(void) {
// the exit may come from a crash on any thread: give up after a second rather than wait on a lock held there
for (int i = 0; i < 100; i++) {
if (plat_mutex_trylock(&lock)) {
if (dll && S.changed) put_back(), rtss_put_back_done(&S);
if (dll && (S.changed || owed)) put_back(), rtss_put_back_done(&S);
plat_mutex_unlock(&lock);
return;
}