Transaction in Python node question

I’m trying to create a node that can change the linestyle of a filled region passed into it. I currently have a working script that takes ina graphic style and a filled region and changes the linestyle, this requires a transaction. It work’s properly when run in manual mode, but when put into automatic mode, the transaction runs over and over and over again, until I force close the whole project. Is this normal for a python node that contains a transaction in it? All transactions must be used in manual mode? Or is there something that is wrong with my code. Here it is:

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

Import RevitAPI

clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *

Import DocumentManager and TransactionManager

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference(‘System’)
from System import *

Import ToDSType(bool) extension method

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
#The inputs to this node will be stored as a list in the IN variables.
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
#These are my inputs for the Node
gStyle = IN[0]
filledRegion = IN[1]

#Declared Variables
output = list()
count = 0
for i in filledRegion:
count = count + 1

#Check each filled region
TransactionManager.Instance.EnsureInTransaction(doc)

for i in range(0, count):
UnwrapElement(filledRegion[i]).SetLineStyleId(ElementId(gStyle[0].Id))
output.append(filledRegion[i])

TransactionManager.Instance.TransactionTaskDone()
#Assign your output to the OUT variable.
OUT = output

 

If needed, I can post the DYN and a sample Revit File. Any help would be awesome. Thanks.