Unable connect as the client id is already in use. Retry with a unique client id.

1,042 views
Skip to first unread message

Viraj Patel

unread,
Apr 18, 2012, 8:55:40 AM4/18/12
to ibpy-d...@googlegroups.com
As discussed and fixed here:

http://code.google.com/p/ibpy/issues/detail?id=2

If you try to reconnect to TWS following a disconnect and the socket hasn't been shut down, you get the following error:

## error:
code=326 message=Unable connect as the client id is already in use. Retry
with a unique client id.


Since the above fix was on a very old version of IB API, I rebuilt IBpy based on the 9.64 TWS.  When I try to apply the above fix manually to the eDisconnect() function in EClientSocket, I found that the code has changed so I'm not sure what to modify.  Here is the old code and the fixed version next to it (scroll down until you see green highlight)

http://code.google.com/p/ibpy/source/diff?spec=svn292&r=292&format=side&path=/trunk/ib/ext/EClientSocket.py

Notice this line was added after "if self.m_socket is not None:"

self.m_socket.shutdown(self.m_socket.SHUT_RDWR)

But in my EClientSocket.py, my eDisconnect function looks like this:

    def eDisconnect(self):
        if self.m_dos is None:
            return
        self.m_connected = False
        self.m_serverVersion = 0
        self.m_TwsTime = ""
        dos = self.m_dos
        self.m_dos = None
        self.reader = self.m_reader
        self.m_reader = None
        try:
            if self.reader is not None:
                self.reader.interrupt()
        except (Exception, ), e:
            pass
        try:
            if dos is not None:
                # dos.shutdown(dos.SHUT_RDWR)    TRIED ADDING THIS LINE BUT DIDN'T WORK
                dos.close()
        except (Exception, ), e:
            pass


Any help would be much appreciated!

-Viraj

Aripplet

unread,
May 8, 2012, 4:35:55 PM5/8/12
to IbPy Discuss
got the same error msg now,

you could try to call con.disconnect() before con.connect()

I guess there is no isConnected() function in this API.

meanwhile, you could manually assign another cliendId before
connection,
like
con.clientId = 18,
con.connect()

as TWS supports up to 9 API client at the same time.

On Apr 18, 8:55 am, Viraj Patel <vp2...@gmail.com> wrote:
> As discussed and fixed here:
>
> http://code.google.com/p/ibpy/issues/detail?id=2
>
> If you try to reconnect to TWS following a disconnect and the socket hasn't
> been shut down, you get the following error:
>
> ## error:
> code=326 message=Unable connect as the client id is already in use. Retry
> with a unique client id.
>
> Since the above fix was on a very old version of IB API, I rebuilt IBpy
> based on the 9.64 TWS.  When I try to apply the above fix manually to the
> eDisconnect() function in EClientSocket, I found that the code has changed
> so I'm not sure what to modify.  Here is the old code and the fixed version
> next to it (scroll down until you see green highlight)
>
> http://code.google.com/p/ibpy/source/diff?spec=svn292&r=292&format=si...
Reply all
Reply to author
Forward
0 new messages