-
Notifications
You must be signed in to change notification settings - Fork 655
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
Modifying a mutable object (like Map) in a duplicated channel affects all instances #2660
Comments
I've hit this problem as well, is there a workaround? |
The workaround is to clone the map as shown in the second example. |
Ah sorry, clearly not paying enough attention! Using
|
Good thing to add to the docs under a section on troubleshooting or gotchas. |
Been thinking about this issue. Fundamentally the mutable object needs to be cloned, the question is whether Nextflow should do it automatically or the user should do it when they need it. I am leaning towards the latter approach. For Nextflow to perform a deep copy every time a channel is referenced (i.e. implicit So I think the best solution is to just use a functional style that doesn't require mutation. Here is your example reworked to produce a new map without mutating or using ch_input.map { meta ->
meta + [id: 'foo']
} See this article |
From the linked article:
that means it is equivalent to using clone and the modifying the state. On one hand, style-wise, I agree that your suggestion is nice since it can modify multiple keys in one statement. On the other hand, it may not be immediately obvious to every reader of the code that values of the LHS for keys in RHS get overwritten by RHS' values. Personally, I would then probably prefer to implement a 'merge' function with explanatory docstring just as in the article and use that. |
Hey guys, just want to note in this thread as well that .clone() does not work on maps that carry a groupKey. This works:
This does not work:
|
@Midnighter I think the @diego-rt Would the |
Actually yes, the Btw, also another thing that seems to be missing AFAIK is a way to remove the groupKey from an object. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
In order to keep this alive, just wanted to comment that I've started using the |
Bug report
Expected behavior and actual behavior
I don't know if this is a bug or just a huge gotcha. Either way, I've been affected by it multiple times and so have others. DSL2 is supposed to automatically copy/duplicate channels when they get re-used multiple times. In general, this works perfectly fine. However, when the channel contains a mutable objects like a
Map
and those get modified in place, this affects all channel instances. I don't know the underlying code but I guess for memory reason channels get copied only shallowly, i.e., they reference the same objects. This can lead to more or less subtle bugs in pipelines and surprising behavior.Steps to reproduce the problem
Consider the following workflow:
This produces below output where all
id
keys contain the'foo'
value. If instead you clone the map, the output is more in line with what I expect.Program output
Output for the "cloned" version:
Environment
164269382420.04~97db1bb-UbuntuThe text was updated successfully, but these errors were encountered: