数据库
首页 > 数据库> > Python MySQLdb连接被多线程共享引发的内核segfault段错误

Python MySQLdb连接被多线程共享引发的内核segfault段错误

作者:互联网

Python celery Worker exited prematurely: signal 11 (SIGSEGV) --一种解决方案
Python libmysqlclient segfault (MySQLdb)
Python多线程共享Mysql连接出错?
python mysqldb多线程问题
python自制简易mysql连接池的实现示例
MySQLdb、Mysqlclient、PyMySQL 三个python的MySQL库的比较和总结

问题

线上环境发现error
image.png

billiard.exceptions.WorkerLostError: Worker exited prematurely: signal 11 (SIGSEGV).
Process 'Worker-47' pid:25883 exited with 'signal 11 (SIGSEGV)'

resolve_monitor_alarms这个celery任务在worker执行时出现未知异常,并导致worker进程结束。

错误是signal 11 (SIGSEGV),这是一个linux signal信号,signal 11标识Segmentation violation,即段异常,涉及到linux内核的数据异常。

排查

这是一个系统内核的错误,那我们首先去看服务器的系统日志。
/var/log/messages

因为我们的任务是每小时的整点执行一次,缩小范围直接看每小时0分的日志。

经过简单的分析,发现每小时都会有这样一条错误日志
image.png

image.png

kernel: python[6406]: segfault at 1b8 ip 00007f24ea928fc8 sp 00007f24d3ffdc20 error 4 in libmysqlclient.so.18.0.0[7f24ea8f7000+2de000]

某个python进程在使用libmysqlclient库的使用遇到segfault段错误

那么问题就定位到mysql了,看下这个任务的具体代码

@classmethod
def alarm_resolved(cls, alarm_source_obj):
    """蓝监监控告警恢复"""

    def _update_bk_monitor_alarms_status_thread(_alarm_list, _semaphore, _cursor):
        # 线程量增加
        _semaphore.acquire()
        try:
            # ...
            sql = "xxx"
            logger.debug(sql)
            _cursor.execute(sql)
            rows = _cursor.fetchall()

            # ...

        except Exception as error:
            logger.exception("_update_bk_monitor_alarms_status_thread error, detail info: " "[error: %s]" % error)

        finally:
            # 线程量减少
            _semaphore.release()

    # ...
    conn = MySQLdb.connect(host=host, user=user, password=password, database=database, charset="utf8")
    cursor = conn.cursor()
    # ...

    # 分批去数据库获取告警事件状态, 分批的原因是mysql query查询大小有限制
    once_query_num = 500
    semaphore = threading.BoundedSemaphore(4)
    threads = []
    for alarm_list in [
        active_alarms[i : i + once_query_num] for i in range(0, len(active_alarms), once_query_num)  # noqa
    ]:
        t = threading.Thread(target=_update_bk_monitor_alarms_status_thread, args=(alarm_list, semaphore, cursor))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    cursor.close()
    conn.close()

使用的MySQLdb模块,打开一个数据库连接,然后把游标共享给多个线程去查询数据库。

这里其实是mysqlclient模块,MySQLdb的fork版本用来兼容python3

我试着给线程的开始和结束打了log,分析发现有一部分线程执行失败了没有结束log。

所以应该是某个子线程拿着游标去查询数据库时引发了内核segfault。

在StackOverflow上找到了如下解决办法:
image.png
为子线程开启自己的mysql连接

经过测试,确实解决了问题。

分析

很明显MySQLdb打开的连接不能被多线程共享,可能会引发内核问题。

看一下官方文档的解释:

threadsafety
Integer constant stating the level of thread safety the interface supports. This is set to 1, which means: Threads may share the module.

The MySQL protocol can not handle multiple threads using the same connection at once. Some earlier versions of MySQLdb utilized locking to achieve a threadsafety of 2. While this is not terribly hard to accomplish using the standard Cursor class (which uses mysql_store_result()), it is complicated by SSCursor (which uses mysql_use_result(); with the latter you must ensure all the rows have been read before another query can be executed. It is further complicated by the addition of transactions, since transactions start when a cursor execute a query, but end when COMMIT or ROLLBACK is executed by the Connection object. Two threads simply cannot share a connection while a transaction is in progress, in addition to not being able to share it during query execution. This excessively complicated the code to the point where it just isn't worth it.

The general upshot of this is: Don't share connections between threads. It's really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned.

For threaded applications, try using a connection pool. This can be done using the Pool module.

有一个叫threadsafety的参数,控制线程安全的级别。
它被设置为1,意味着多线程只能共享模块。

  • 0 多线程不能共享模块
  • 1 多线程可以共享模块
  • 2 多线程可以共享模块和连接
  • 3 多线程可以共享模块、连接和游标

但是看了一下这只是一个声明,MySQLdb只支持threadsafety=1,也就是多线程只能共享模块,不能共享连接和游标。原因大概是2和3的实现较为昂贵和复杂。

MySQLdb不建议我们多线程共享连接,不值得花费精力去实现,并且可能带来性能问题,因为mysql server为每一个连接都会运行一个单独的线程。

MySQLdb建议我们使用连接池去缓存多个连接,缓存池初始化后,每个线程都从缓存池中去获取自己的连接,并在线程结束归还到连接池。

MySQLdb还指明,如果多个线程同时使用了一个mysql连接,我们的mysql client很可能会崩溃死掉。

解决办法

所以对我们的问题,有两个解决办法:

前者性能好,后者代码实现快。

总结

MySQLdb连接线程不安全导致的问题,需要为线程分配单独的数据库连接。

django.db是用连接池实现的,不会有问题。
但我们直接使用一些mysql模块的时候要注意线程安全问题。

标签:Python,segfault,MySQLdb,线程,mysql,共享,多线程,连接
来源: https://www.cnblogs.com/libaiyun/p/16572132.html