Support Questions

Find answers, ask questions, and share your expertise

Apache NiFi already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed

avatar
New Contributor
I am getting following error in NiFi `ExecuteScript` processor.
 
```java
18:07:09 UTCERROR9e1d9c5e-0188-1000-ffff-fffffbc25817
10.42.62.250:8080ExecuteScript[id=9e1d9c5e-0188-1000-ffff-fffffbc25817] ExecuteScript[id=9e1d9c5e-0188-1000-ffff-fffffbc25817] failed to process due to org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: java.lang.IllegalStateException: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed in <script> at line number 37
- Caused by: javax.script.ScriptException: java.lang.IllegalStateException: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed in <script> at line number 37
- Caused by: Traceback (most recent call last):
  File "<script>", line 37, in <module>
at org.apache.nifi.controller.repository.StandardProcessSession.validateRecordState(StandardProcessSession.java:3715)
at org.apache.nifi.controller.repository.StandardProcessSession.validateRecordState(StandardProcessSession.java:3710)
at org.apache.nifi.controller.repository.StandardProcessSession.transfer(StandardProcessSession.java:2349)
at jdk.internal.reflect.GeneratedMethodAccessor2736.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.IllegalStateException: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed
 
- Caused by: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed; rolling back session: {}
18:07:09 UTCERROR9e1d9c5e-0188-1000-ffff-fffffbc25817
10.42.62.250:8080ExecuteScript[id=9e1d9c5e-0188-1000-ffff-fffffbc25817] Processing failed: org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: java.lang.IllegalStateException: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed in <script> at line number 37
- Caused by: javax.script.ScriptException: java.lang.IllegalStateException: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed in <script> at line number 37
- Caused by: Traceback (most recent call last):
  File "<script>", line 37, in <module>
at org.apache.nifi.controller.repository.StandardProcessSession.validateRecordState(StandardProcessSession.java:3715)
at org.apache.nifi.controller.repository.StandardProcessSession.validateRecordState(StandardProcessSession.java:3710)
at org.apache.nifi.controller.repository.StandardProcessSession.transfer(StandardProcessSession.java:2349)
at jdk.internal.reflect.GeneratedMethodAccessor2736.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.IllegalStateException: java.lang.IllegalStateException: StandardFlowFileRecord[uuid=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1686706804350-73290, container=default, section=586], offset=6556, length=39642],offset=0,name=40d42213-2c7a-49a2-8c5d-5a7fc58bad2d,size=39642] already in use for an active callback or an InputStream created by ProcessSession.read(FlowFile) has not been closed
```
 
I am trying to run following script in NiFi `ExecuteScript` processor
 
```
import xml.etree.ElementTree as ET
import os
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
def find_pattern_in_xml(file_path, target_string):
    tree = ET.ElementTree(ET.fromstring(file_path))
    root = tree.getroot()
 
    found_elements = []
 
    # Iterate through all elements in the XML tree
    for element in root.iter():
        # Check if the target string is present in the element text
        if target_string in element.text:
            found_elements.append(element)
 
    return found_elements
flowFile = session.get()
if flowFile is not None:
    try:
        stream_content = session.read(flowFile)
        text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
        # flowfile_path = flowFile.getAttribute('sourceFileName')
        target_string = "~Indv"
        found_elements = find_pattern_in_xml(flowfile_path, target_string)
 
        # Print the found elements
        for element in found_elements:
            session.remove(flowFile)
            print(element.tag, element.text)
    except Exception as e:
        # Log the error and route the flowfile to failure
        log.error("Error processing flowfile"+ str(e))
        session.transfer(flowFile, REL_FAILURE)
session.commit()
 
```
2 REPLIES 2

avatar
Community Manager

@pdesh Welcome to the Cloudera Community!

To help you get the best possible solution, I have tagged our NiFi experts @MattWho and @cotopaul  who may be able to assist you further.

Please keep us updated on your post, and we hope you find a satisfactory solution to your query.


Regards,

Diana Torres,
Community Moderator


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

avatar
Super Collaborator

This is likely what's generating the error... You're saying remove the Flow file in a loop and should only be done once.

 

for element in found_elements:

            session.remove(flowFile)

            print(element.tag, element.text)