MaxKB 调试高级编排时,会出现 connection already closed 报错!

高级编排中,使用到了函数节点,在调试高级编排时报错,查看执行详情,错误显示 connection already closed.
查看服务运行报错日志,如下:
POST /api/application/chat_workflow/open HTTP/1.1" 200 88
Traceback (most recent call last):
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 308, in _cursor
return self._prepare_cursor(self.create_cursor(name))
^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/postgresql/base.py”, line 330, in create_cursor
cursor = self.connection.cursor()
^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.InterfaceError: connection already closed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/opt/maxkb/app/apps/application/flow/workflow_manage.py”, line 363, in hand_event_node_result
current_result = node_result_future.result()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/maxkb/app/apps/application/flow/workflow_manage.py”, line 176, in result
raise self.e
File “/opt/maxkb/app/apps/application/flow/workflow_manage.py”, line 400, in run_node_future
node.valid_args(node.node_params, node.workflow_params)
File “/opt/maxkb/app/apps/application/flow/i_step_node.py”, line 142, in valid_args
self.node_params_serializer.is_valid(raise_exception=True)
File “/opt/maxkb/app/apps/application/flow/step_node/function_lib_node/i_function_lib_node.py”, line 32, in is_valid
f_lib = QuerySet(FunctionLib).filter(id=self.data.get(‘function_lib_id’)).first()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/models/query.py”, line 1057, in first
for obj in queryset[:1]:
File “/opt/py3/lib/python3.11/site-packages/django/db/models/query.py”, line 398, in iter
self._fetch_all()
File “/opt/py3/lib/python3.11/site-packages/django/db/models/query.py”, line 1881, in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/models/query.py”, line 91, in iter
results = compiler.execute_sql(
^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/models/sql/compiler.py”, line 1560, in execute_sql
cursor = self.connection.cursor()
^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 330, in cursor
return self._cursor()
^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 307, in _cursor
with self.wrap_database_errors:
File “/opt/py3/lib/python3.11/site-packages/django/db/utils.py”, line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 308, in _cursor
return self._prepare_cursor(self.create_cursor(name))
^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/postgresql/base.py”, line 330, in create_cursor
cursor = self.connection.cursor()
^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.InterfaceError: connection already closed
Traceback (most recent call last):
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 308, in _cursor
return self._prepare_cursor(self.create_cursor(name))
^^^^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “/opt/py3/lib/python3.11/site-packages/django/db/backends/postgresql/base.py”, line 330, in create_cursor
cursor = self.connection.cursor()
^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.InterfaceError: connection already closed

查看上述错误日志,结合搜索到的信息“Django 会自动管理数据库连接,但在某些异步或并发场景下,可能会遇到连接关闭的问题”
猜测应该是高级编排工作流,并发执行工作节点,导致和数据库连接出现问题,然后执行函数节点时,需要查询具体的函数节点code,此时就会报上述错误。
我把服务重启后,再执行调试就能正常运行,不过个人猜想,当服务并发量上去之后,还是会出现上述问题。
希望能在后续版本中优化解决上述性能所引起的问题!

管理员大大,看看这个帖子呀!

目前还不能完全确定是django管理的原因,建议先增加pgsql允许的最大连接数后再测试。
1、进入pgsql容器:docker exec -it pgsql sh
2、编辑配置文件:vim /var/lib/postgresql/data/postgresql.conf
3、将max_connections参数值设置为1000,并保存修改
4、退出容器,在服务器执行docker restart pgsql命令重启即可

嗯嗯,我也进行了这方面的尝试
通过命令查看pgsql的最大连接数和当前活动连接数
show max_connections;
select count(*) from pg_stat_activity;
查询到最大连接数为默认值:100
当前活动连接数为:26
从查询到的结果来分析,尝试增加最大连接数意义不大。
而且我这里报错,但是我页面其他接口访问请求也是正常的,这些接口也需要查询pgsql数据库。