{
  "id": "python/pytest-warnings-as-errors-unclosed-socket",
  "signature": "FAILED tests/test_network.py::test_connect - pytest.PytestUnraisableExceptionWarning: Exception ignored in: <socket.socket fd=5>\nResourceWarning: unclosed <socket.socket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 54321), raddr=('127.0.0.1', 8080)>",
  "signature_zh": "FAILED tests/test_network.py::test_connect - pytest.PytestUnraisableExceptionWarning: 忽略的异常：<socket.socket fd=5>\nResourceWarning: 未关闭的 <socket.socket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 54321), raddr=('127.0.0.1', 8080)>",
  "regex": "FAILED\\ tests/test_network\\.py::test_connect\\ \\-\\ pytest\\.PytestUnraisableExceptionWarning:\\ Exception\\ ignored\\ in:\\ <socket\\.socket\\ fd=5>\\\nResourceWarning:\\ unclosed\\ <socket\\.socket\\ fd=5,\\ family=AddressFamily\\.AF_INET,\\ type=SocketKind\\.SOCK_STREAM,\\ proto=0,\\ laddr=\\('127\\.0\\.0\\.1',\\ 54321\\),\\ raddr=\\('127\\.0\\.0\\.1',\\ 8080\\)>",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "pytest is configured with filterwarnings = error, so ResourceWarning from an unclosed socket (e.g., a connection not closed in a fixture or test) is promoted to an error and fails the test.",
  "root_cause_type": "generic",
  "root_cause_zh": "pytest 配置了 filterwarnings = error，因此未关闭的 socket（例如 fixture 或测试中未关闭的连接）产生的 ResourceWarning 被提升为错误并导致测试失败。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Hides real resource leaks in production code and can mask other warnings-as-errors that you do want to catch.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "gc.collect() may run before the socket's __del__ or may not close the socket deterministically; the warning can still fire during interpreter shutdown.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "import socket\nimport pytest\n\n@pytest.fixture\ndef conn():\n    s = socket.create_connection(('127.0.0.1', 8080))\n    yield s\n    s.close()\n\ndef test_connect(conn):\n    ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from contextlib import closing\n\ndef test_connect():\n    with closing(socket.create_connection(('127.0.0.1', 8080))) as s:\n        ...",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-07-03",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}