Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve: premature close at onclosenexttick in Node.js Server?

How to solve this error, I am running my Node.js with AWS IoT then it at times shows this error:

      throw er; // Unhandled 'error' event
      ^

Error: How  (/home/ec2-user/work/nodejs_27_01/node_modules/end-of-stream/index.js:54:                                                                                                     86)
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on DeviceClient instance at:
    at MqttClient.<anonymous> (/home/ec2-user/work/nodejs_27_01/node_modules/aws-iot-device-sdk/                                                                                                     device/index.js:772:15)
    at MqttClient.emit (events.js:333:22)
    at MqttClient.EventEmitter.emit (domain.js:485:12)
    at TLSSocket.f (/home/ec2-user/work/nodejs_27_01/node_modules/once/once.js:25:25)
    at onclosenexttick (/home/ec2-user/work/nodejs_27_01/node_modules/end-of-stream/index.js:54:                                                                                                     73)


2 Answers

There can be multiple reasons for this:

Multiple Connections with same ClientId

The clientId can only be used for one connection at a time. If you connect with the same clientId while another connection is established, the older connection gets dropped (which leads to the premature close error) and the new connection is established.

The client is using a client ID that is already in use. In this case, the client that is already connected will be disconnected [...]. (Source)

Permissions

This error can happen if a device (mqtt.Client from aws-iot-device-sdk-js) does not hold the correct permissions to connect and/or publish/subscribe/receive messages on a given topic.

See here for more documentation: https://docs.aws.amazon.com/iot/latest/developerguide/pub-sub-policy.html

The policy should look like this (example shows a Cloudformation Iot Policy resource):

MyIotThingsPolicy:
  Type: AWS::IoT::Policy
  Properties:
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Action: iot:Connect
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:client/",
                                 "${iot:ClientId}"] ]
        - Action: iot:Receive
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/",
                                "${iot:ClientId}/eg/your/broadcast/topic"] ]
        - Action: iot:Subscribe
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topicfilter/",
                                 "${iot:ClientId}/eg/your/broadcast/topic"] ]
        - Action: iot:Publish
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/",
                                 "${iot:ClientId}/eg/your/publish/topic"] ]

The !Join is necessary since Cloudformation would try to resolve ${iot:ClientId}, which is a runtime value, and not known during deployment.

Troubleshooting

  • As Sandeep Patel also suggests, you should implement an error callback if you want to act on this situation on client side:

    device.on('error', (error) => {
      // error.message might be 'premature close'
    });
    
  • You can also look for disconnectReason on the topic $aws/events/presence/disconnected/<clientId>, see: https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html

like image 176
Simon Avatar answered Nov 02 '25 07:11

Simon


While testing, I had attached policy with full "Action": "iot:*" permissions, but missed to activate the newly created certificate. Please go to IoT core service, Secure --> Certificates and verify the certificate attached to <Thing> is activated if you get error Error: premature close after you have confirmed the connection to end point with :

telnet <your-iot-endpoint> 8883.

See the attached image for the options available with each certificate on above specified page.

enter image description here

like image 21
Midhun KM Avatar answered Nov 02 '25 06:11

Midhun KM