记一次 ABP VNext 进程崩溃。错误源 System.ObjectDisposedException: Cannot access a disposed context instance.
正在做 excel 导入功能,代码在自己的机器上正常运行,部署到 CentOS 后只要调用这块代码进程就直接崩掉,以下是问题代码:
using (var stream = new MemoryStream())
{
ImportQuestionsFile.CopyTo(stream);
var dt = NPOIHelper.ImportExceltoDt(stream);
var list = new List<CreateUpdateQuestionDto>();
dt.AsEnumerable().ToList().ForEach(x =>
{
...MORE CODE...
list.Add(question);
});
_questionAppService.InsertManyAsync(list);
return NoContent();
}
开始以为和以前一样是进程占用内存太大导致,结果一顿排查进程不是被系统 kill 的。
Logs 里面又没有日志,幸好使用了 Supervisor 守护进程,在它的日志里面找到的问题根源:
Unhandled exception. System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'AssociationDbContext'.
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.OnTracked(InternalEntityEntry internalEntityEntry, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)
根据日志分析发现是 DbContext 被 disposed 。最后排查是因为 _questionAppService.InsertManyAsync(list); 这个异步方法没有加 await 程序就不会等待。但不知道为啥进程会崩溃。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!