Skip to content

Commit

Permalink
Merge pull request #4212 from balena-io/fix-race
Browse files Browse the repository at this point in the history
patch: hold request for metadata while waiting for flasher
  • Loading branch information
flowzone-app[bot] authored Apr 26, 2024
2 parents 73afb2f + 2dfa795 commit fa8220d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/gui/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,29 @@ export let requestMetadata: any;
// start the api and spawn the child process
spawnChildAndConnect({
withPrivileges: false,
}).then(({ emit, registerHandler }) => {
// start scanning
emit('scan', {});

// make the sourceMetada awaitable to be used on source selection
requestMetadata = async (params: any): Promise<SourceMetadata> => {
emit('sourceMetadata', JSON.stringify(params));

return new Promise((resolve) =>
registerHandler('sourceMetadata', (data: any) => {
resolve(JSON.parse(data));
}),
);
};

registerHandler('drives', (data: any) => {
setDrives(JSON.parse(data));
})
.then(({ emit, registerHandler }) => {
// start scanning
emit('scan', {});

// make the sourceMetada awaitable to be used on source selection
requestMetadata = async (params: any): Promise<SourceMetadata> => {
emit('sourceMetadata', JSON.stringify(params));

return new Promise((resolve) =>
registerHandler('sourceMetadata', (data: any) => {
resolve(JSON.parse(data));
}),
);
};

registerHandler('drives', (data: any) => {
setDrives(JSON.parse(data));
});
})
.catch((error: any) => {
throw new Error(`Failed to start the flasher process. error: ${error}`);
});
});

let popupExists = false;

Expand Down
8 changes: 8 additions & 0 deletions lib/gui/app/components/source-selector/source-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,14 @@ export class SourceSelector extends React.Component<
// this will send an event down the ipcMain asking for metadata
// we'll get the response through an event

// FIXME: This is a poor man wait while loading to prevent a potential race condition without completely blocking the interface
// This should be addressed when refactoring the GUI
let retriesLeft = 10;
while (requestMetadata === undefined && retriesLeft > 0) {
await new Promise((resolve) => setTimeout(resolve, 1050)); // api is trying to connect every 1000, this is offset to make sure we fall between retries
retriesLeft--;
}

metadata = await requestMetadata({ selected, SourceType, auth });

if (!metadata?.hasMBR && this.state.warning === null) {
Expand Down

0 comments on commit fa8220d

Please sign in to comment.