如何在Linux上强制关闭套接字? [英] How to force closing socket on Linux?

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

问题描述

对于 原因,我想尝试一些新的 - 使用一些系统调用关闭套接字。



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



这是我做的 - 有另一个开始的线程 - 定时器。所以,在一个特定的超时(让我们说10秒),如果没有响应,我想关闭套接字。 MYSQL结构具有成员 net ,也是一个结构体,并保存 fd 。但是当我尝试这样做:

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

没有任何反应。 shutdown close 的返回值为 0 ,但套接字仍然是打开的(因为在60秒后等待,有一个从DB返回的结果,这意味着mysql客户端仍然在等待响应从数据库。



任何

我试图关闭套接字,但这是实际的问题 - 我不能终止查询,也不关闭连接,没有什么,我不想等待整个超时,这是20分钟和30秒,或某事这就是为什么我在寻找一个暴力..:/

解决方案

但是确保你取消/终止任何正在运行的事务我不熟悉MySQL C API,但我想象有一种方法来检查是否有任何活动的连接/查询你可能无法关闭套接字只是因为仍然有事情在运行,他们需要被带到一些解决状态,无论是提交还是回滚。我会从那里开始,看看会发生什么。你真的不想关闭套接字暴力风格,如果你有任何待处理,因为你的数据将不会在一个可靠的状态后 - 你不知道什么事务成功和哪些没有,虽然我会认为如果连接失败,MySQL将回滚任何挂起的事务。



EDIT
通过Googling MySQL停止失控查询,一致似乎是要求MySQL终止线程的失控/长时间运行查询使用

  KILL thread-id 



我想象线程ID在MySQL数据中可用结构包含套接字。



EDIT#2
显然MySQL提供了将会重新启动已关闭连接的故障安全机制,因此强制关闭套接字不会实际终止查询。一旦你关闭它,MySQL将打开另一个,并尝试完成查询。关闭此选项将允许您关闭套接字并导致查询终止。



下面的注释显示了如何找到答案,以及涉及的思考过程。 / p>

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

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.

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 );

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.

Any ideas?

Thanks

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.. :/

解决方案

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.

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

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.

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天全站免登陆