python-plexapi icon indicating copy to clipboard operation
python-plexapi copied to clipboard

"Invalid library sectionID: None" after PlexPartialObject.reload()

Open krin-san opened this issue 1 year ago • 5 comments

Describe the Bug

For some reason explicitly calling reload() on PlexPartialObject causes this object to lose librarySectionID value which causes any data editing attempts to fail with exception "Invalid library sectionID: None".

I have tried to compare Music Album genres for all albums to a local list, but .search() was loading only 2 genres for each album. To get the full list of genres, album has to be reloaded but after reloading it is not editable.

Code Snippets

album = plex.library.section('Music').searchAlbums(limit=1, filters=...)[0]
print(album.section()) // <MusicSection:x:Music>
album.reload()
print(album.section()) // NotFound: Invalid library sectionID: None

Expected Behavior

Reloading an object shouldn't cause it to lose the connection to the library section it was found in and should remain editable.

Additional Context

Example stack trace:

File /opt/conda/lib/python3.11/site-packages/plexapi/base.py:647, in PlexPartialObject.saveEdits(self)
    645 edits = self._edits
    646 self._edits = None
--> 647 self._edit(**edits)
    648 return self

File /opt/conda/lib/python3.11/site-packages/plexapi/base.py:586, in PlexPartialObject._edit(self, **kwargs)
    583 if 'type' not in kwargs:
    584     kwargs['type'] = utils.searchType(self._searchType)
--> 586 self.section()._edit(items=self, **kwargs)
    587 return self

File /opt/conda/lib/python3.11/site-packages/plexapi/base.py:670, in PlexPartialObject.section(self)
    668 def section(self):
    669     """ Returns the :class:`~plexapi.library.LibrarySection` this item belongs to. """
--> 670     return self._server.library.sectionByID(self.librarySectionID)

File /opt/conda/lib/python3.11/site-packages/plexapi/library.py:111, in Library.sectionByID(self, sectionID)
    109     return self._sectionsByID[sectionID]
    110 except KeyError:
--> 111     raise NotFound(f'Invalid library sectionID: {sectionID}') from None

NotFound: Invalid library sectionID: None

Operating System and Version

docker.io/jupyter/minimal-notebook@sha256:9aef9e53b7d8bdc7bdd9a2824fe6287081131bc02bf03edc1450ab9413646837

Plex Media Server Version

1.40.0.7998

Python Version

3.11.6

PlexAPI Version

4.15.10

krin-san avatar Mar 04 '24 21:03 krin-san

Found a quick workaround: store librarySectionID value before reload() and restore it after:

librarySectionID = album.librarySectionID
album.reload()
album.librarySectionID = librarySectionID

krin-san avatar Mar 04 '24 21:03 krin-san

I can't reproduce the issue.

>>> from plexapi.server import PlexServer
>>> plex = PlexServer()
>>> album = plex.library.section("Music").searchAlbums(limit=1)[0] 
>>> album.section()
<MusicSection:11:Music>
>>> album.reload()
<Album:651:Layers>
>>> album.section()
<MusicSection:11:Music>

>>> plex.version
'1.40.1.8120-6dc7f7fd8'
>>> import plexapi
>>> plexapi.__version__
'4.15.10'

JonnyWong16 avatar Mar 05 '24 01:03 JonnyWong16

Then what's the difference? I don't think that Plex version difference alone causes it. I repeated the same test in Python shell on both Jupyter container and on MacOS to exclude environment differences.

>>> from plexapi.server import PlexServer
>>> plex = PlexServer('http://x.x.x.x:32400', ...)
>>> album = plex.library.section("Music").searchAlbums(limit=1)[0]
>>> album.section()
<MusicSection:6:Music>
>>> album.reload()
<Album:110968>
>>> album.section()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.11/site-packages/plexapi/base.py", line 670, in section
    return self._server.library.sectionByID(self.librarySectionID)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/plexapi/library.py", line 111, in sectionByID
    raise NotFound(f'Invalid library sectionID: {sectionID}') from None
plexapi.exceptions.NotFound: Invalid library sectionID: None

>>> plex.version
'1.40.0.7998-c29d4c0c8'
>>> import plexapi
>>> plexapi.__version__
'4.15.10'

krin-san avatar Mar 05 '24 08:03 krin-san

I honestly don't know.

But it looks like your album doesn't have a title <Album:110968>. Maybe that's a clue?

JonnyWong16 avatar Mar 05 '24 14:03 JonnyWong16

No, it's not. It's one of Various Artists albums with incorrect tags. Issue is still reproducible if I randomize search result with .searchAlbums(limit=1, sort='random') and run multiple times.

krin-san avatar Mar 05 '24 18:03 krin-san