Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataBufferUtils.write(Publisher, Path) loses context #28933

Closed
kzander91 opened this issue Aug 5, 2022 · 2 comments
Closed

DataBufferUtils.write(Publisher, Path) loses context #28933

kzander91 opened this issue Aug 5, 2022 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@kzander91
Copy link
Contributor

kzander91 commented Aug 5, 2022

Very similar to #27517, but it looks like this issue has not been fixed for the Path variant of DataBufferUtils.write, see the following code (similar to the example snippet from #27517 but writing to a Path instead of an OutputStream):

DataBufferUtils.read(new ByteArrayResource("test".getBytes()), new NettyDataBufferFactory(new PooledByteBufAllocator()), 1024)
        .transformDeferredContextual((f, ctx) -> {
            System.out.println("1: " + ctx.getOrDefault("key", "EMPTY"));
            return f;
        })
        .transform(f -> DataBufferUtils.write(f, Path.of("test")))
        .transformDeferredContextual((f, ctx) -> {
            System.out.println("2: " + ctx.getOrDefault("key", "EMPTY"));
            return f;
        })
        .contextWrite(Context.of("key", "TEST"))
        .subscribe();

Expected result:

2: TEST
1: TEST

Actual result:

2: TEST
1: EMPTY

The context is lost here:

write(source, channel).subscribe(DataBufferUtils::release,
sink::error,
sink::success);

Passing sink's context into the subscribe() call would fix this isssue:

write(source, channel).subscribe(DataBufferUtils::release,
		sink::error,
		sink::success,
		Context.of(sink.contextView()));

Environment:

  • Spring Boot 2.7.2
  • Spring Framework 5.3.22
  • Project Reactor 3.4.21
@kzander91 kzander91 changed the title DataBufferUtils.write(Publisher, Path) loses context DataBufferUtils.write(Publisher, Path) loses context Aug 5, 2022
@kzander91 kzander91 changed the title DataBufferUtils.write(Publisher, Path) loses context DataBufferUtils.write(Publisher, Path) loses context Aug 5, 2022
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 5, 2022
@kzander91
Copy link
Contributor Author

Our workaround until this is fixed is to manually propagate the context:

Flux<DataBuffer> bufs = ...;
Path path = ...;
Mono<Void> write = Mono.deferContextual(ctx -> DataBufferUtils.write(bufs.contextWrite(ctx), path));

@poutsma poutsma self-assigned this Aug 15, 2022
@poutsma poutsma added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 16, 2022
@poutsma poutsma added this to the 5.3.23 milestone Aug 16, 2022
@poutsma
Copy link
Contributor

poutsma commented Aug 16, 2022

Fixed! Thanks for spotting this, @kzander91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants