You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行HTTP请求后调用Prisma findMany()崩溃,错误码0xC0000005求助

问题描述

单独调用Prisma查询方法(如prisma.some.findMany())时运行正常,但在执行HTTP请求(axios、@nestjs/axios或node-fetch)后再调用该方法,程序直接崩溃,退出码为-1073741819 (0xC0000005)。

复现代码

正常运行的代码:

// 无异常
await prisma.some.findMany()

触发崩溃的代码:

// 先执行HTTP请求
const some = await axios.get('https://some.com/some')
console.log(some)

console.log('before')
// 执行此查询后程序崩溃
await prisma.some.findMany()
console.log('after') // 该行日志不会输出

日志输出

{/* HTTP请求返回内容 */}
before
Process finished with exit code -1073741819 (0xC0000005)

环境信息

  • Windows 10
  • prisma: 4.8.0
  • @prisma/client: 4.8.0
  • @nestjs/core: 9.0.0
  • @nestjs/axios: 1.0.1
  • axios: ^1.3.3
  • node-fetch: 2
  • typescript: 4.7.4

原因分析

退出码0xC0000005是Windows系统的访问违规错误,表示程序试图访问未授权的内存区域。结合场景,核心原因是:

  1. Prisma 4.8.0的Windows版本bug:该版本在Windows环境下,与HTTP客户端的底层网络依赖存在内存交互冲突,触发内存访问异常。
  2. 异步上下文冲突:HTTP请求改变了Node.js事件循环状态,若Prisma客户端实例管理不当(如多实例),会加剧内存冲突问题。

解决办法

1. 升级Prisma版本(优先推荐)

Prisma在4.10+版本中修复了多个Windows环境下的内存冲突问题,直接升级到最新稳定版:

npm install prisma@latest @prisma/client@latest

升级后重新生成客户端:

npx prisma generate

2. 确保Prisma客户端单例化

在NestJS中,通过单例服务管理Prisma实例,避免重复创建导致的上下文冲突:

// prisma.service.ts
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }
}

在模块中注册为全局提供者,确保全应用共用一个实例。

3. 临时规避方案(无法升级时使用)

  • 禁用HTTP客户端的自动重定向/代理:减少底层网络库的内存操作干扰
    const some = await axios.get('https://some.com/some', {
      maxRedirects: 0,
      proxy: false
    })
    
  • 切换到Node.js原生HTTP模块:替换第三方HTTP库验证问题
    import https from 'https';
    
    function fetchData(url: string): Promise<any> {
      return new Promise((resolve, reject) => {
        https.get(url, (res) => {
          let data = '';
          res.on('data', (chunk) => { data += chunk; });
          res.on('end', () => resolve(JSON.parse(data)));
        }).on('error', reject);
      });
    }
    
    const some = await fetchData('https://some.com/some');
    

内容的提问来源于stack exchange,提问作者Ignat neko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:01:19