Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop engine failed when app quit #1453

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: stop engine failed when app quit
agalwood committed Apr 27, 2023
commit 003e9ffccba0e83b57d69b2c0512283daa3cf84c
29 changes: 18 additions & 11 deletions src/main/Application.js
Original file line number Diff line number Diff line change
@@ -151,8 +151,10 @@ export default class Application extends EventEmitter {
}

async stopEngine () {
logger.info('[Motrix] stopEngine===>')
try {
await this.engineClient.shutdown({ force: true })
logger.info('[Motrix] stopEngine.setImmediate===>')
setImmediate(() => {
this.engine.stop()
})
@@ -509,22 +511,27 @@ export default class Application extends EventEmitter {
this.windowManager.destroyWindow(page)
}

async stop () {
stop () {
try {
await this.shutdownUPnPManager()

this.energyManager.stopPowerSaveBlocker()

await this.stopEngine()

this.trayManager.destroy()
const promises = [
this.stopEngine(),
this.shutdownUPnPManager(),
this.energyManager.stopPowerSaveBlocker(),
this.trayManager.destroy()
]

return promises
} catch (err) {
logger.warn('[Motrix] stop error: ', err.message)
}
}

async stopAllSettled () {
await Promise.allSettled(this.stop())
}

async quit () {
await this.stop()
await this.stopAllSettled()
app.exit()
}

@@ -644,7 +651,7 @@ export default class Application extends EventEmitter {

this.updateManager.on('will-updated', async (event) => {
this.windowManager.setWillQuit(true)
await this.stop()
await this.stopAllSettled()
})

this.updateManager.on('update-error', (event) => {
@@ -654,7 +661,7 @@ export default class Application extends EventEmitter {
}

async relaunch () {
await this.stop()
await this.stopAllSettled()
app.relaunch()
app.exit()
}
1 change: 1 addition & 0 deletions src/main/Launcher.js
Original file line number Diff line number Diff line change
@@ -183,6 +183,7 @@ export default class Launcher extends EventEmitter {
app.on('will-quit', () => {
logger.info('[Motrix] will-quit')
if (global.application) {
logger.info('[Motrix] will-quit.application.stop')
global.application.stop()
}
})
2 changes: 2 additions & 0 deletions src/main/core/EnergyManager.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import logger from './Logger'
let psbId
export default class EnergyManager {
startPowerSaveBlocker () {
logger.info('[Motrix] EnergyManager.startPowerSaveBlocker', psbId)
if (psbId && powerSaveBlocker.isStarted(psbId)) {
return
}
@@ -14,6 +15,7 @@ export default class EnergyManager {
}

stopPowerSaveBlocker () {
logger.info('[Motrix] EnergyManager.stopPowerSaveBlocker', psbId)
if (typeof psbId === 'undefined' || !powerSaveBlocker.isStarted(psbId)) {
return
}
1 change: 1 addition & 0 deletions src/main/core/Engine.js
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ export default class Engine {
}

stop () {
logger.info('[Motrix] engine.stop.instance')
if (this.instance) {
this.instance.kill()
this.instance = null
3 changes: 2 additions & 1 deletion src/main/ui/TrayManager.js
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@ import is from 'electron-is'

import { APP_RUN_MODE, APP_THEME } from '@shared/constants'
import { getInverseTheme } from '@shared/utils'
import logger from '../core/Logger'
import { getI18n } from './Locale'
import {
translateTemplate,
flattenMenuItems,
updateStates
} from '../utils/menu'
import { convertArrayBufferToBuffer } from '../utils/index'
// import logger from '../core/Logger'

let tray = null
const { platform } = process
@@ -357,6 +357,7 @@ export default class TrayManager extends EventEmitter {
}

destroy () {
logger.info('[Motrix] TrayManager.destroy')
if (tray) {
this.unbindEvents()
}