Skip to content

Commit

Permalink
fix: memory leak + tournament client
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark authored and KotRikD committed Feb 15, 2024
1 parent df14e91 commit ba8edbf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
24 changes: 15 additions & 9 deletions packages/find-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ use sysinfo::System;
fn process_by_name(mut cx: FunctionContext) -> JsResult<JsValue> {
let sys = System::new_all();
let process_name = cx.argument::<JsString>(0)?.value(&mut cx);
let obj = cx.empty_object();
let result_array = JsArray::new(&mut cx, 0);

let mut index = 0;

for process in sys.processes_by_name(&process_name) {
let id = cx.number(process.pid().as_u32());
let name = cx.string(process.name().to_string());
let cmd = cx.string(process.cmd()[0].to_string());
if process.name() == &process_name {
let id = cx.number(process.pid().as_u32());
let name = cx.string(process.name().to_string());
let cmd = cx.string(process.cmd()[0].to_string());

let _ = obj.set(&mut cx, "pid", id);
let _ = obj.set(&mut cx, "name", name);
let _ = obj.set(&mut cx, "cmd", cmd);
let obj = cx.empty_object();
let _ = obj.set(&mut cx, "pid", id);
let _ = obj.set(&mut cx, "name", name);
let _ = obj.set(&mut cx, "cmd", cmd);

return Ok(obj.upcast());
let _ = result_array.set(&mut cx, index, obj);
index += 1;
}
}

Ok(cx.null().upcast())
Ok(result_array.upcast())
}

#[neon::main]
Expand Down
56 changes: 26 additions & 30 deletions packages/tosu/src/objects/instanceManager/instanceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { process_by_name } from '@tosu/find-process';

import { OsuInstance } from './osuInstance';

export const OSU_REGEX = /.*osu!\.exe.*/g;

export class InstanceManager {
osuInstances: {
[key: number]: OsuInstance;
Expand All @@ -23,36 +21,34 @@ export class InstanceManager {
delete this.osuInstances[pid];
}

private lookupProcess() {
const osuProcess = process_by_name('osu!.exe');
if (osuProcess == null || osuProcess?.pid == null) {
return null;
}

if (osuProcess.pid in this.osuInstances) {
return 'old';
}

const osuInstance = new OsuInstance(osuProcess.pid);
if (osuProcess.cmd.includes('-spectateclient')) {
osuInstance.setIsTourneySpectator(true);
}

osuInstance.emitter.on('onDestroy', this.onProcessDestroy.bind(this));
osuInstance.emitter.on(
'onResolveFailed',
this.onProcessDestroy.bind(this)
);

this.osuInstances[osuProcess.pid] = osuInstance;
osuInstance.start();

return osuProcess.pid;
}

async runWatcher() {
while (true) {
this.lookupProcess();
const osuProcesses = process_by_name('osu!.exe');

for (const process of osuProcesses || []) {
if (process.pid in this.osuInstances) {
// dont deploy not needed instances
continue;
}

const osuInstance = new OsuInstance(process.pid);
if (process.cmd.includes('-spectateclient')) {
osuInstance.setIsTourneySpectator(true);
}

osuInstance.emitter.on(
'onDestroy',
this.onProcessDestroy.bind(this)
);
osuInstance.emitter.on(
'onResolveFailed',
this.onProcessDestroy.bind(this)
);

this.osuInstances[process.pid] = osuInstance;
osuInstance.start();
}

await sleep(5000);
}
}
Expand Down

0 comments on commit ba8edbf

Please sign in to comment.