Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

How to clean up nicely #767

Closed
hardillb opened this issue Nov 5, 2021 · 4 comments
Closed

How to clean up nicely #767

hardillb opened this issue Nov 5, 2021 · 4 comments

Comments

@hardillb
Copy link

hardillb commented Nov 5, 2021

Calling client.search(base,opts, function(err, res){}) with a malformed filter (e.g. (uid=)) means you can't clean up the client connection without triggering an error as the outstanding search message's error callback is triggered.

When client.ubind() called there is still a message on the queue. Has the search not returned an err then I could have used the messageID with client.abandon() but I can't see how to clear the message queue without an id.

Example

const ldapjs = require('ldapjs');

let client = ldapjs.createClient({
	url: "ldap://ldap.forumsys.com"
})

client.on('close', function(){
	console.log("connection closed")
})

client.on('connect', function(){
	console.log("connected")

  const options = {
    filter: 'uid=',
    scope: 'sub'
  }


  client.search("dc=example,dc=com",options, function(err, res) {
    if (err) {
      console.log(err)
      client.unbind();
    } else {
      res.on('searchEntry', function(entry){

      })
      res.on('error', function(error){

      })
      res.on('end', function(result){

      })
    }
  });
})
@jsumners
Copy link
Member

The unbind() will terminate the connection to the LDAP server. Therefore, no new messages will be received. What else should be done?

@hardillb
Copy link
Author

hardillb commented Mar 27, 2022

It's been a LONG time since I raised this, but I think my problem is that calling client.unbind() as part of dealing with the search error triggers the callback a second time again with a different error. I have updated the testcase again to show the problem a little clearer.

const ldapjs = require('ldapjs');
let client = ldapjs.createClient({
	url: "ldap://ldap.forumsys.com"
})

client.on('close', function(){
	console.log("connection closed")
})

client.on('connect', function(){
	console.log("connected")

  const options = {
    filter: 'uid=',
    scope: 'sub'
  }

  client.search("dc=example,dc=com",options, function(err, res) {
    if (err) {
      console.log("Error in search callback")
      console.log(err)
      try {
        console.log("calling unbind")
        client.unbind();
      } catch (error) {
        console.log("second error")
        console.log(error)
      }
    } else {
      res.on('searchEntry', function(entry){

      })
      res.on('error', function(error){

      })
      res.on('end', function(result){

      })
    }
  });
})

Which give the following output:

connected
Error in search callback
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

  assert.ok(len)

    at Writer._ensure (/home/hardillb/temp/ldap/node_modules/asn1/lib/ber/writer.js:298:10)
    at Writer.writeBuffer (/home/hardillb/temp/ldap/node_modules/asn1/lib/ber/writer.js:157:8)
    at EqualityFilter._toBer (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/filters/equality_filter.js:57:7)
    at EqualityFilter.toBer (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/filters/filter.js:51:16)
    at SearchRequest._toBer (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/messages/search_request.js:122:11)
    at SearchRequest.LDAPMessage.toBer (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/messages/message.js:93:36)
    at Client._sendSocket (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/client.js:1278:31)
    at Client._send (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/client.js:1169:17)
    at sendRequest (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/client.js:611:17)
    at Client.search (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/client.js:640:5) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: 0,
  expected: true,
  operator: '=='
}
calling unbind
connection closed
Error in search callback
ConnectionError: 1__ldap://ldap.forumsys.com closed
    at /home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/client.js:1083:17
    at /home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/message-tracker/index.js:113:7
    at Map.forEach (<anonymous>)
    at Object.purgeMessages [as purge] (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/message-tracker/index.js:110:14)
    at Client._onClose (/home/hardillb/temp/ldap/node_modules/ldapjs/lib/client/client.js:1081:11)
    at Object.onceWrapper (node:events:640:26)
    at Socket.emit (node:events:520:28)
    at TCP.<anonymous> (node:net:687:12) {
  lde_message: '1__ldap://ldap.forumsys.com closed',
  lde_dn: null
}
calling unbind

Why is the search callback being triggered twice?

@jsumners
Copy link
Member

🤔

Likely a bug. If I'm not mistaken, there's an internal tracking of pending messages. This queue could be emptied in the unbind invocation prior to actually sending the unbind message. But I could be mis-remembering things.

Ideally, we'd add a failing integration test in out integration suite and solve from there.

@jsumners
Copy link
Member

👋

On February 22, 2023, we released version 3 of this library. As a result, we are closing this issue/pull request.

Please see issue #839 for more information, including how to proceed if you feel this closure is in error.

@ldapjs ldapjs locked as resolved and limited conversation to collaborators Feb 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants