[issue24775] Python client failing to connect to server but completing as if successful

Seán Kelleher report at bugs.python.org
Sat Aug 1 22:47:40 CEST 2015


New submission from Seán Kelleher:

I have a Go server that listens to a port, runs a Python client to connect to the port as a subcommand, and reads from the client. However, the client (as follows) will occasionally run to completion without connecting to the port, but without raising an exception:

    import socket
    import sys

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    [addr, port] = sys.argv[1].split(':')
    sock.connect((addr, int(port)))
    try:
        sock.send("hello")
    finally:
        sock.close()

    print "done."

`server.go` follows:

	package main

	import (
		"log"
		"net"
		"os"
		"os/exec"
	)

	func main() {
		ln, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)})
		if err != nil {
			log.Fatalf("%v", err)
		}
		defer ln.Close()

		cmd := exec.Command(
			"python",
			"client.py",
			ln.Addr().(*net.TCPAddr).String(),
		)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		if err := cmd.Start(); err != nil {
			log.Fatalf("%v", err)
		}
		defer cmd.Process.Kill()

		go func() {
			log.Printf("command exited with: %v", cmd.Wait())
			log.Printf("closing listener: %v", ln.Close())
		}()

		conn, err := ln.Accept()
		if err != nil {
			log.Fatalf("%v", err)
		}

		buf := make([]byte, 1024)
		n, err := conn.Read(buf)
		log.Println(string(buf[:n]))
	}

When the connection is successful, the output is as expected:

    done.
    2015/08/01 21:03:50 hello

A failed connection, by contrast, gives no indication from Python that the command failed (`done.` is output), but it is evident that the connection was not established:

    done.
    2015/08/01 20:56:55 command exited with: <nil>
    2015/08/01 20:56:55 closing listener: <nil>
    2015/08/01 20:56:55 accept tcp4 127.0.0.1:42550: use of closed network connection
    exit status 1

>From this, it appears as though the Python client thinks it has established a connection, because neither the `connect` nor the `send` call raise an exception.

This behaviour is corrected in Python 3 (all runs look like the first instance), so it appears to be local to Python 2.7.

----------
components: Library (Lib)
messages: 247826
nosy: Seán Kelleher
priority: normal
severity: normal
status: open
title: Python client failing to connect to server but completing as if successful
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24775>
_______________________________________


More information about the Python-bugs-list mailing list