如何强制在Linux上关闭插座? [英] How to force closing socket on Linux?

查看:155
本文介绍了如何强制在Linux上关闭插座?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关<一个href=\"http://stackoverflow.com/questions/4254059/how-to-set-a-timeout-for-mysql-query-using-c-api\">THIS因此,我想新的尝试一些 - 使用一些系统调用关闭套接字。

For THIS reason, I want to try something new - close the socket using some system call.

在两个词的情况 - 不能设置MySQL库的查询超时(的C API,请参考链接获取更多信息),所以我想尝试关闭套接字看到该库将如何反应。也许这不是一个好主意,但还是想尝试一下。

The situation in two words - can't set query timeout of the mysql library (the C API, refer to the link for more info), so I want to try closing the socket to see how the library will react. Probably this is not a good idea, but still wanna try it.

下面是我做了什么 - 还有另外一个线程开始 - 一个计时器。因此,一个特定的超时后(如10秒),如果没有反应,我想关闭套接字。 MySQL的结构有成员,这也是一个结构,并持有 FD 。但是,当我尝试这样做:

Here's what I've done - there's another started thread - a timer. So, after a specific timeout (let's say 10 second), if there's no response, I want to close the socket. The MYSQL struct has member net, that is also a struct, and holds the fd. But when I try to do this:

shutdown( m_pOwner->m_ptrDBConnection->m_mysql.net.fd, SHUT_RDWR );
close( m_pOwner->m_ptrDBConnection->m_mysql.net.fd );

没有任何反应。从关机返回的值关闭 0 ,但套接字仍然打开(因为60秒等待,还有来自DB返回的结果,这意味着,MySQL客户端仍在等待从DB响应。

nothing happens. The returned values from shutdown and close are 0, but the socket is still opened (because after 60sec waiting, there's a returned result from the DB, that means that the mysql client is still waiting for response from the DB.

任何想法?

感谢

修改 - 是的,有一个运行的事务,而我试图关闭套接字。但是,这是实际的问题 - 我不能终止查询,也没有关闭连接,没事的,我不想等到整个超时,这是20分钟和30秒,或者这样的事情。这就是为什么我在寻找一个强力..:/

EDIT - Yes, there's a running transaction, while I'm trying to close the socket. But this is the actual problem - I cannot terminate the query, nor to close the connection, nothing, and I don't wanna wait the whole timeout, which is 20min and 30 sec, or something like this. That's why I'm looking for a brute-force.. :/

推荐答案

只是瞎猜,但要确保你取消/终止任何运行的事务。我不熟悉MySQL的C API,但我想有一种方法来检查,如果有任何活动的连接/查询。你可能不能够简单地关闭套接字,因为有仍在运行的东西,他们需要带来一些解决的状态,是提交或回滚。我会从这儿开始,看看会发生什么。你真的不想关闭套接字蛮力的风格,如果您有任何未决的,因为反正你的数据不会在一个可靠的国家之后 - 你不知道什么是交易成功,哪些没有,虽然我可以想象的MySQL将回滚任何未决的交易如果连接突然失败。

Just a shot in the dark, but make sure you cancel/terminate any running transactions. I'm not familiar with the MySQL C API, but I would imagine there is a way to check if there are any active connections/queries. You may not be able to close the socket simply because there are still things running, and they need to be brought to some "resolved" state, be that either committed or rolled back. I would begin there and see what happens. You really don't want to shutdown the socket "brute force" style if you have anything pending anyway because your data would not be in a reliable "state" afterwards - you would not know what transactions succeeded and which ones did not, although I would imagine that MySQL would rollback any pending transactions if the connection failed abruptly.

修改
从我通过谷歌搜索的MySQL停止失控查询找到了,共识似乎是问MySQL的使用终止失控/长时间运行的查询的线程

EDIT: From what I have found via Googling "MySQL stopping runaway query", the consensus seems to be to ask MySQL to terminate the thread of the runaway/long-running query using

KILL thread-id

我会想象线程ID提供给您在包含插座MySQL的数据结构。你可能想试试这个,虽然IIRC这样做需要超级用户权限的。

I would imagine that the thread ID is available to you in the MySQL data structure that contains the socket. You may want to try this, although IIRC to do so requires super user priviledges.

编辑#2
显然MySQL提供了故障安全机制,将重新启动的封闭连接,以便有力地关闭插座实际上不会终止查询。一旦你关闭它,MySQL将打开另一个并尝试完成查询。关闭这个选项将允许你关闭套接字,导致查询终止。

EDIT #2: Apparently MySQL provides a fail-safe mechanism that will restart a closed connection, so forcefully shutting down the socket will not actually terminate the query. Once you close it, MySQL will open another and attempt to complete the query. Turning this off will allow you to close the socket and cause the query to terminate.

答案是如何发现下图显示的注释,其中涉及的思维过程。

The comments below show how the answer was found, and the thought process involved therein.

这篇关于如何强制在Linux上关闭插座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆