Skip to content

LdapTemplate searchForStream leaks file descriptor #1169

@prthakre

Description

@prthakre

We are using ````LdapTemplate.searchForStream()``` along with pooling2 .

try (Stream<T> results =
                 ldapTemplate.searchForStream(query, mapper))
        {
            return results
                .filter(Objects::nonNull)
                .toList();
        }

With successive requests, the count of DirContextType.READ_ONLY contexts continues to grow, ultimately failing with Timeout waiting for idle object

However, changing the code to use LdapTemplate.search(), doesn't result in similar failure.

try

        {
            List<T> results = ldapTemplate.search(query, mapper);
            return results
                .stream()
                .filter(Objects::nonNull)
                .toList();
        }

As per implementation of searchForStream(LdapQuery query, Function<SearchResult, T> mapper) in spring-ldap/core/src/main/java/org/springframework/ldap/core/LdapTemplate.java closeContextAndNamingEnumeration is never called if results is null

        NamingEnumeration<SearchResult> results = unchecked(() -> {
		LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
		return ctx.search(base, encodedFilter, searchControls);
	});
	if (results == null) {
                closeContextAndNamingEnumeration(ctx, null); // this is currently not present.
		return Stream.empty();
	}
	return StreamSupport
		.stream(Spliterators.spliteratorUnknownSize(CollectionUtils.toIterator(results), Spliterator.ORDERED),
				false)
		.map((nameClassPair) -> unchecked(() -> mapper.apply(nameClassPair)))
		.filter(Objects::nonNull)
		.onClose(() -> closeContextAndNamingEnumeration(ctx, results));

Version information:

JDK: 21
spring-boot: 3.5.3
spring-ldap: 3.3.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions