'NoneType' object has no attribute 'append'

rs7683
I have got this error few times now and this usually comes after some exception is caught in my code. It happens after timeout exception also, which could happen anytime due to slow network also so I am worried if I can ever use Kite API effectively, kindly assist.
Below is where this error occurs:
\venv\lib\site-packages\autobahn\websocket\protocol.py", line 601, in onMessageFrameData
self.frame_data.append(payload)

Please let me know what should I do to avoid triggering this bug or would this be resolved soon?
Let me know if you need more details.
Tagged:
  • rs7683
    I can provide the entire stacktrace if needed.
  • rs7683
    Looked into protocol.py file and found that frame_data is set to None how it could be set to it's default value [] instead of none. I am not sure but something is triggering onMEssageFrameEnd method and resulting frame_data to NoneType but it should be set as [] to clear data. None is not the right way to clear a list.

    Appreciate any information to avoid this situation or a permanent fix which is as below:

    Change self.frame_data = None
    to self.frame_data =[]
    in code below

    Below is code in protocol.py.
    def onMessageFrameEnd(self):
    """
    Implements :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessageFrameEnd`
    """
    if not self.failedByMe:
    self._onMessageFrame(self.frame_data)

    self.frame_data = None
  • Vivek
    can you send us the stack trace?
  • rs7683
    I tried posting it couple of times but due to very huge message, it says that it will appear after it is approved. But hope you got it.
  • rs7683
    I am able to replicate this exception now by just printing a boolean value at each tick which fails as boolean needs to be converted to string.
    That "print" keeps failing on each tick and after a random number of failures, I get failure from "protocol.py" and after that execution also stops. It failed after 54 failures once and 124 next time. I have put a counter to check this.

    Even though this bug of "frame_data = None" (could be fixed by assigning [] instead of None)is not fixed, I need a way to continue execution of my program.

    Please suggest.
  • rs7683
    As there is limit on length of message, I will try to post complete stacktrace in multiple messages from top to bottom
  • rs7683
    Unhandled Error
    Traceback (most recent call last):
    File "\venv\lib\site-packages\kiteconnect\ticker.py", line 532, in connect
    reactor.run(**opts)
    File "\venv\lib\site-packages\twisted\internet\base.py", line 1283, in run
    self.mainLoop()
    File "\venv\lib\site-packages\twisted\internet\base.py", line 1295, in mainLoop
    self.doIteration(t)
    File "\venv\lib\site-packages\twisted\internet\selectreactor.py", line 143, in doSelect
    _logrun(selectable, _drdw, selectable, method)
    --- ---
    File "\venv\lib\site-packages\twisted\python\log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
    File "\venv\lib\site-packages\twisted\python\log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
    File "\venv\lib\site-packages\twisted\python\context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
    File "\venv\lib\site-packages\twisted\python\context.py", line 85, in callWithContext
    return func(*args,**kw)
    File "\venv\lib\site-packages\twisted\internet\selectreactor.py", line 154, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, method=="doRead")
    File "\venv\lib\site-packages\twisted\internet\posixbase.py", line 258, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
    File "\venv\lib\site-packages\twisted\internet\tcp.py", line 519, in connectionLost
    self._commonConnection.connectionLost(self, reason)
    File "\venv\lib\site-packages\twisted\internet\tcp.py", line 327, in connectionLost
    protocol.connectionLost(reason)
    File "\venv\lib\site-packages\twisted\protocols\tls.py", line 398, in connectionLost
    self._flushReceiveBIO()
    File "\venv\lib\site-packages\twisted\protocols\tls.py", line 295, in _flushReceiveBIO
    ProtocolWrapper.dataReceived(self, bytes)
    File "\venv\lib\site-packages\twisted\protocols\policies.py", line 120, in dataReceived
    self.wrappedProtocol.dataReceived(data)
    File "\venv\lib\site-packages\autobahn\twisted\websocket.py", line 290, in dataReceived
    self._dataReceived(data)
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1206, in _dataReceived
    self.consumeData()
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1218, in consumeData
    while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1570, in processData
    fr = self.onFrameData(payload)
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1665, in onFrameData
    self._onMessageFrameData(payload)
    File "\venv\lib\site-packages\autobahn\twisted\websocket.py", line 309, in _onMessageFrameData
    self.onMessageFrameData(payload)
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 601, in onMessageFrameData
    self.frame_data.append(payload)
    builtins.AttributeError: 'NoneType' object has no attribute 'append'
  • rs7683
    rs7683 edited June 2020
    Ok, I figured that problem with pasting stacktrace was not with length but name of folder I kept KITE API. I just removed that and it worked.
    Please check the stacktrace now.
  • rakeshr
    @rs7683
    self.frame_data.append(payload)
    builtins.AttributeError: 'NoneType' object has no attribute 'append'
    We believe, this error seems to be caused while handling another initiating error in your code base. Can you paste here complete stacktrace including initial error?
  • rs7683
    You are right about that. I am able to replicate this error by causing any simple exception on every tick. With same faulty code exception in question appears after random number of exception's occurrence caused by me.

    I am not able to paste exception stacktrace from my code. This forum doesn't approves it. Can I email that?
  • rs7683
    Unhandled Error
    Traceback (most recent call last):
    File "\venv\lib\site-packages\twisted\python\log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
    File "\venv\lib\site-packages\twisted\python\log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
    File "\venv\lib\site-packages\twisted\python\context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
    File "\venv\lib\site-packages\twisted\python\context.py", line 85, in callWithContext
    return func(*args,**kw)
    --- ---
  • rs7683
    File "\venv\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
    why = getattr(selectable, method)()
    File "\venv\lib\site-packages\twisted\internet\tcp.py", line 243, in doRead
    return self._dataReceived(data)
    File "\venv\lib\site-packages\twisted\internet\tcp.py", line 249, in _dataReceived
    rval = self.protocol.dataReceived(data)
    File "\venv\lib\site-packages\twisted\protocols\tls.py", line 330, in dataReceived
    self._flushReceiveBIO()
    File "\venv\lib\site-packages\twisted\protocols\tls.py", line 295, in _flushReceiveBIO
    ProtocolWrapper.dataReceived(self, bytes)
    File "\venv\lib\site-packages\twisted\protocols\policies.py", line 120, in dataReceived
    self.wrappedProtocol.dataReceived(data)
    File "\venv\lib\site-packages\autobahn\twisted\websocket.py", line 290, in dataReceived
    self._dataReceived(data)
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1206, in _dataReceived
    self.consumeData()
  • rs7683
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1218, in consumeData
    while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1578, in processData
    fr = self.onFrameEnd()
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 1703, in onFrameEnd
    self._onMessageEnd()
    File "\venv\lib\site-packages\autobahn\twisted\websocket.py", line 318, in _onMessageEnd
    self.onMessageEnd()
    File "\venv\lib\site-packages\autobahn\websocket\protocol.py", line 627, in onMessageEnd
    self._onMessage(payload, self.message_is_binary)
    File "\venv\lib\site-packages\autobahn\twisted\websocket.py", line 321, in _onMessage
    self.onMessage(payload, isBinary)
    File "\venv\lib\site-packages\kiteconnect\ticker.py", line 71, in onMessage
    self.factory.on_message(self, payload, is_binary)
    File "\venv\lib\site-packages\kiteconnect\ticker.py", line 670, in _on_message
    self.on_ticks(self, self._parse_binary(payload))
    File "Main.py", line 40, in on_ticks
    Main_Methods.vrs(ticks[0]["last_price"], 1, 15, 75)
    File "\Main_Methods.py", line 58, in vrs
    print("___Order lagane se pehle long_order_placed " + orders_manager.short_order_placed)
    builtins.TypeError: can only concatenate str (not "bool") to str
  • rs7683
    I was able to paste complete stack trace in 3 chunks. Right after this I get the main error I am concerned about. I already shared that stacktrace
  • rs7683
    I tried to bang my head a lot but no vein. I even changed your code to "self.frame_data = []" but now instead of "Nonetype" exception, it just stops execution after it reaches that point.
    BElow is my code where I use on_ticks callback function, how I am replicating it. On each tick, it fails as I am not converting boolean to string while printing and after a random number of time, it stops executing with this ghost situation for me.
    Please assist.

    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    try:
    print("TestingException " + False)

    except Exception as e:
    print("LAst price Printing error", str(e))
    raise
  • rs7683
    As you can see even with exception my program keep running after every tick until it stumbles upon "Nonetype" exception when it stops executing further and I have to terminate my python program as nothing happens after that on consecutive ticks.

    I just need to find a way to keep my execution ongoing, please suggest how.
  • rs7683
    By the way, you can also replicate this in production by just inducing any simple exception in "on_ticks" callback method and it will happen after few ticks(could be big number of ticks also).

    Now this situation is pretty random, my real code doesn't have so many exceptions occurring but there could be network exception (timeout) as well and I have seen it stopping execution after that. Again not always but sometimes as "Nonetype" issue doesn't happen always.

    Let me know, if you want me on the call and show you on screen share.
  • rakeshr
    rakeshr edited June 2020
    @rs7683
    just inducing any simple exception in "on_ticks" callback method and it will happen after few ticks(could be big number of ticks also)
    Don't perform any computation inside on_ticks method, it will block the running thread. You can go through this discussion to know, an alternate ways to perform those computations.
  • rs7683
    Thanks Rakesh. Let me try to do computation in new thread and I will see if same issue is replicated. Will post the results.
    Appreciate your help.
  • rs7683
    I tried below code with "threaded = true" and still end up getting same error after few executions.

    Could you please suggest?

    ----------------------------------------------------------
    try:
    kws.connect(threaded=True)
    except Exception as e:
    print("Authentication failed", str(e))
    raise
    while True:
    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    try:
    print("###Name of thread is " + str(threading.current_thread().getName()))
    test()

    except Exception as e:
    print("LAst price Printing error", str(e))
    raise

    def test():
    @ Test ke andar Name of thread is " + str(threading.current_thread().getName()))
    print("TestingException " + False)

    kws.on_ticks = on_ticks
  • rs7683
    Another thing I noticed is -

    1. Before using "threaded = Ture" entire execution was in MAinThread
    2. After using "threaded = True", now on_ticks and any method called within on_ticks is in Thread-1

    I am not able to run on_ticks and method call within on_ticks with different threads.
  • rs7683
    Hi Rakesh and team, I am able to run on_ticks callback and calculation on those ticks in different threads now.
    Nonetype error doesn't occur now.

    Thanks a lot for your suggestion.
This discussion has been closed.