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

Bug: Cordis 某些框架函数传入的 callback 报错可能导致 Koishi 崩溃或者副作用处理异常 #1254

Closed
CyanChanges opened this issue Nov 1, 2023 · 2 comments
Labels
bug BUG

Comments

@CyanChanges
Copy link

CyanChanges commented Nov 1, 2023

Describe the bug

当你在某些框架函数,
ctx.on('dispose', ()=>{/*...*/}) 传入的 callback 中出现报错,
将会导致后续 dispose 操作不会执行,且再次 dispose 不会继续执行后续 dispose 操作 (在 dispose 前判断了 插件状态)

Steps to reproduce

import { Context } from 'cordis'

const ctx = new Context()

// load a sus plugin that often gets exception when dispose
const fork = ctx.plugin((ctx) => {
    console.log('parent load!')

    ctx.on('dispose', () => {
        console.log('parent dispose')
        throw new Error("Something went wrong here")
    })

    ctx.plugin({ // any disposable register after the dispose handler upside is ok, 
                 // use a plugin with dispose handler as an example.
        name: 'other-disposables',
        apply(ctx: Context) {
            console.log('sub load!')
            ctx.on('dispose', () => {
                console.log('sub dispose') // never calls
            })
        }
    })
})

ctx.start().then()

// In Koishi, stop a plugin run in ensure(), 
// exceptions will be caught, Koishi won't crash
// You won't see any exception output, because no one ctx.on 'internal/warning' event to handle it
ctx.scope.ensure(async () => {
    fork.dispose()
})

// try to dispose again
fork.dispose() // checks status here, so it does nothing here
parent load!
sub load!
parent dispose

Expected behavior

catch 这个异常,继续执行后续 dispose 操作
部分顺序调用或调用到用户函数后续包含副作用处理(console handlers, emit等 callback 的都建议加上 ensure 等 catch 方法防止报错崩溃

Screenshots

No response

Versions

  • OS: Linux CyanArch 6.5.9-zen2-1-zen Use Monorepo #1 ZEN SMP PREEMPT_DYNAMIC Thu, 26 Oct 2023 00:51:53 +0000 x86_64 GNU/Linux
  • Node version: v20.9.0
  • Koishi version: 4.15.3
  • Cordis version: 3.1.2

Additional context

No response

@CyanChanges CyanChanges added the bug BUG label Nov 1, 2023
@CyanChanges
Copy link
Author

(6, 内核版本的井号被转义了

@CyanChanges
Copy link
Author

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug BUG
Projects
None yet
Development

No branches or pull requests

1 participant